date() and/or strftime() broken?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Johnson

    #1

    date() and/or strftime() broken?

    the following script...

    <?php

    $times = array(
    '2004-04-04 00:31:00-06',
    '2004-04-04 01:00:00-06',
    '2004-04-04 01:59:59-06',
    '2004-04-04 02:00:00-06',
    '2004-04-04 03:00:00-06',
    );

    for ($i = 0; $i < count($times); $i++)
    {
    $s = $times[$i];
    $time_t = strtotime($s);
    print "strtotime('$s' ) = $time_t\n";
    print "date('r', $time_t) = \"" . date('r', $time_t) . "\"\n";
    print "\n";
    }

    ?>

    generates this output:

    strtotime('2004-04-04 00:31:00-06') = 1081060260
    date('r', 1081060260) = "Sat, 3 Apr 2004 23:31:00 -0700"

    strtotime('2004-04-04 01:00:00-06') = 1081062000
    date('r', 1081062000) = "Sun, 4 Apr 2004 00:00:00 -0700"

    strtotime('2004-04-04 01:59:59-06') = 1081065599
    date('r', 1081065599) = "Sun, 4 Apr 2004 00:59:59 -0700"

    strtotime('2004-04-04 02:00:00-06') = 1081069200
    date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"

    strtotime('2004-04-04 03:00:00-06') = 1081069200
    date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"



    date() seems to be picking rather arbitrary timezones for output.
    Secondly, as best as I can tell, the fourth output is just patently WRONG
    (or I'm too dense to see what's going on, but I don't think so).

    I often want to calculate a time_t value at 00:00:00 (midnight) for an
    arbitrary input time_t. I had been doing this as:

    $midnight = strtotime(date( 'Y-m-d', $time_t));

    but given the behaviour above, I don't think this works reliably. :(


    The machine this is running on happens to be in MDT right now
    (offset -06) and running PHP version 4.1.0. Can someone help me figure out
    what the hell is going on here (or a *reliable* way to get a time_t value
    for midnight of input strings of the form strtotime() handles)?

    Thanks in advance.
    -ej


  • Pedro Graca

    #2
    Re: date() and/or strftime() broken?

    Erik Johnson wrote:[color=blue]
    > the following script...
    >
    ><?php
    >
    > $times = array(
    > '2004-04-04 00:31:00-06',
    > '2004-04-04 01:00:00-06',
    > '2004-04-04 01:59:59-06',[/color]

    As per information on http://www.timeanddate.com/

    before switching over to MDT the timezone offset was -0700
    [color=blue]
    > '2004-04-04 02:00:00-06',[/color]

    This particular time did not happen.
    MDT started on the second after 2004-04-04 01:59:59 (local time -- MST)
    which was 2004-04-04 03:00:00 (local time -- MDT)
    [color=blue]
    > '2004-04-04 03:00:00-06',
    > );
    >
    > for ($i = 0; $i < count($times); $i++)
    > {
    > $s = $times[$i];
    > $time_t = strtotime($s);
    > print "strtotime('$s' ) = $time_t\n";
    > print "date('r', $time_t) = \"" . date('r', $time_t) . "\"\n";
    > print "\n";
    > }
    >
    > ?>
    >
    > generates this output:
    >
    > strtotime('2004-04-04 00:31:00-06') = 1081060260
    > date('r', 1081060260) = "Sat, 3 Apr 2004 23:31:00 -0700"[/color]

    Wrong input
    [color=blue]
    > strtotime('2004-04-04 01:00:00-06') = 1081062000
    > date('r', 1081062000) = "Sun, 4 Apr 2004 00:00:00 -0700"[/color]

    Wrong input
    [color=blue]
    > strtotime('2004-04-04 01:59:59-06') = 1081065599
    > date('r', 1081065599) = "Sun, 4 Apr 2004 00:59:59 -0700"[/color]

    Wrong input
    [color=blue]
    > strtotime('2004-04-04 02:00:00-06') = 1081069200
    > date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"[/color]

    Wrong input
    [color=blue]
    > strtotime('2004-04-04 03:00:00-06') = 1081069200
    > date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"[/color]

    OK :)
    [color=blue]
    > date() seems to be picking rather arbitrary timezones for output.
    > Secondly, as best as I can tell, the fourth output is just patently WRONG
    > (or I'm too dense to see what's going on, but I don't think so).[/color]

    Confused by timezone changes (and their respective offset)? :)
    [color=blue]
    > I often want to calculate a time_t value at 00:00:00 (midnight) for an
    > arbitrary input time_t. I had been doing this as:
    >
    > $midnight = strtotime(date( 'Y-m-d', $time_t));
    >
    > but given the behaviour above, I don't think this works reliably. :([/color]

    Let's abolish DST !!!!!

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Erik Johnson

      #3
      Re: date() and/or strftime() broken?

      "Pedro Graca" <hexkid@hotpop. com> wrote:[color=blue]
      > Confused by timezone changes (and their respective offset)? :)[/color]

      Yes. That's it (DOH!). ;) I forgot this had happened - I happened to be
      looking at date/time stuff anyway.
      Was very stressed out yesterday with a number of other things - not thinking
      clearly.
      Thank you for your reply. :)

      -ej

      in message news:c4tupm$2n7 np5$1@ID-203069.news.uni-berlin.de...[color=blue]
      > Erik Johnson wrote:[color=green]
      > > the following script...
      > >
      > ><?php
      > >
      > > $times = array(
      > > '2004-04-04 00:31:00-06',
      > > '2004-04-04 01:00:00-06',
      > > '2004-04-04 01:59:59-06',[/color]
      >
      > As per information on http://www.timeanddate.com/
      >
      > before switching over to MDT the timezone offset was -0700
      >[color=green]
      > > '2004-04-04 02:00:00-06',[/color]
      >
      > This particular time did not happen.
      > MDT started on the second after 2004-04-04 01:59:59 (local time -- MST)
      > which was 2004-04-04 03:00:00 (local time -- MDT)
      >[color=green]
      > > '2004-04-04 03:00:00-06',
      > > );
      > >
      > > for ($i = 0; $i < count($times); $i++)
      > > {
      > > $s = $times[$i];
      > > $time_t = strtotime($s);
      > > print "strtotime('$s' ) = $time_t\n";
      > > print "date('r', $time_t) = \"" . date('r', $time_t) . "\"\n";
      > > print "\n";
      > > }
      > >
      > > ?>
      > >
      > > generates this output:
      > >
      > > strtotime('2004-04-04 00:31:00-06') = 1081060260
      > > date('r', 1081060260) = "Sat, 3 Apr 2004 23:31:00 -0700"[/color]
      >
      > Wrong input
      >[color=green]
      > > strtotime('2004-04-04 01:00:00-06') = 1081062000
      > > date('r', 1081062000) = "Sun, 4 Apr 2004 00:00:00 -0700"[/color]
      >
      > Wrong input
      >[color=green]
      > > strtotime('2004-04-04 01:59:59-06') = 1081065599
      > > date('r', 1081065599) = "Sun, 4 Apr 2004 00:59:59 -0700"[/color]
      >
      > Wrong input
      >[color=green]
      > > strtotime('2004-04-04 02:00:00-06') = 1081069200
      > > date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"[/color]
      >
      > Wrong input
      >[color=green]
      > > strtotime('2004-04-04 03:00:00-06') = 1081069200
      > > date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"[/color]
      >
      > OK :)
      >[color=green]
      > > date() seems to be picking rather arbitrary timezones for output.
      > > Secondly, as best as I can tell, the fourth output is just patently[/color][/color]
      WRONG[color=blue][color=green]
      > > (or I'm too dense to see what's going on, but I don't think so).[/color]
      >
      > Confused by timezone changes (and their respective offset)? :)
      >[color=green]
      > > I often want to calculate a time_t value at 00:00:00 (midnight) for[/color][/color]
      an[color=blue][color=green]
      > > arbitrary input time_t. I had been doing this as:
      > >
      > > $midnight = strtotime(date( 'Y-m-d', $time_t));
      > >
      > > but given the behaviour above, I don't think this works reliably. :([/color]
      >
      > Let's abolish DST !!!!!
      >
      > --
      > USENET would be a better place if everybody read: : mail address :
      > http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
      > http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
      > http://www.expita.com/nomime.html : to 10K bytes :[/color]


      Comment

      Working...