unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

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

    #1

    unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

    Hello. I noticed a strange thing while using strtotime() and date()
    functions in combination to generate from MySQL into a readable format.

    By default, the MySQL date field will be 0000-00-00 00:00:00 When I
    pass this to strtotime() to generate the timestamp, and then pass it to
    the date function, it generates 30-11-1999.

    [snippet]
    <?
    print ( "<br> strtotime for '0000-00-00 00:00:00' : " . strtotime (
    '0000-00-00 00:00:00' ) );
    print ( "<br> date for the above : " . date ( "d-m-Y", strtotime (
    '0000-00-00 00:00:00' ) ) );
    ?>
    [/snippet]

    I am on PHP 5.0.2
    Why this unusual behaviour? Or is it an expected behaviour?

    Suggestions/comments/flamings solicited.

    Regards,
    Rithish.

  • Oli Filth

    #2
    Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

    Rithish wrote:[color=blue]
    > Hello. I noticed a strange thing while using strtotime() and date()
    > functions in combination to generate from MySQL into a readable format.
    >
    > By default, the MySQL date field will be 0000-00-00 00:00:00 When I
    > pass this to strtotime() to generate the timestamp, and then pass it to
    > the date function, it generates 30-11-1999.[/color]
    [color=blue]
    >From http://dev.mysql.com/doc/refman/5.1/en/datetime.html:[/color]
    "The supported range is '1000-01-01 00:00:00' to '9999-12-31
    23:59:59'..."

    0000-00-00 is not in the supported range of times in MySQL.


    --
    Oli

    Comment

    • Rithish

      #3
      Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

      Oli Filth wrote:[color=blue]
      > 0000-00-00 is not in the supported range of times in MySQL.
      >[/color]

      Ok. But why does strtotime() throw a timestamp for 30-11-1999?

      Regards,
      Rithish.

      Comment

      • Juan José Gutiérrez de QuevedoPérez

        #4
        Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00'input

        El 21 Apr 2006 08:30:26 -0700
        Rithish escribió:
        [color=blue]
        > Hello. I noticed a strange thing while using strtotime() and date()
        > functions in combination to generate from MySQL into a readable
        > format.[/color]

        and why don't you just use date_format() function from mysql i.e.:

        SELECT date_format('00 00-00-00','%d/%m/%Y') as formatted_date;

        http://dev.mysql.com/doc/refman/5.0/...functions.html

        --
        Juan José Gutiérrez de Quevedo
        Director Técnico (juanjo@iteisa. com)
        ITEISA (http://www.iteisa.com)
        942544036 - 637447953

        Comment

        • Oli Filth

          #5
          Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

          Rithish said the following on 21/04/2006 16:44:[color=blue]
          > Oli Filth wrote:[color=green]
          >> 0000-00-00 is not in the supported range of times in MySQL.
          >>[/color]
          >
          > Ok. But why does strtotime() throw a timestamp for 30-11-1999?[/color]

          Oops, sorry, misread your original code, I thought you were referring to
          MySQL functionality.

          0000-00-00 isn't a valid date string at all in PHP (i.e. you can't have
          the zero-th day of the zero-th month). I guess anything that
          strtotime() returns is purely arbitrary, although one would expect it to
          return FALSE, as the manual implies.


          --
          Oli

          Comment

          • Gordon Burditt

            #6
            Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

            >Hello. I noticed a strange thing while using strtotime() and date()[color=blue]
            >functions in combination to generate from MySQL into a readable format.
            >
            >By default, the MySQL date field will be 0000-00-00 00:00:00 When I
            >pass this to strtotime() to generate the timestamp, and then pass it to
            >the date function, it generates 30-11-1999.[/color]

            Generally, I prefer to avoid the PHP date functions as much as
            possible, except maybe when it's dealing with the current date, in
            favor of the MySQL date/time functions. Why? The PHP functions
            don't have enough range to cover the life expectancy of a living
            person. That's pretty constraining.
            [color=blue]
            >[snippet]
            ><?
            >print ( "<br> strtotime for '0000-00-00 00:00:00' : " . strtotime (
            >'0000-00-00 00:00:00' ) );
            >print ( "<br> date for the above : " . date ( "d-m-Y", strtotime (
            >'0000-00-00 00:00:00' ) ) );
            >?>
            >[/snippet]
            >
            >I am on PHP 5.0.2
            >Why this unusual behaviour? Or is it an expected behaviour?[/color]

            The allowed range for *PHP* timestamps is the same range as UNIX
            timestamps. This may vary from system to system depending on whether
            a time_t is considered signed (1901 - 2038) or unsigned (1970 -
            2106). In any case the year 0 is way out of range. The Year 2038
            problem will start to bite shortly, given the existence of 30-year
            mortgages.

            Gordon L. Burditt

            Comment

            • ljb

              #7
              Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

              rithish@gmail.c om wrote:[color=blue]
              > Hello. I noticed a strange thing while using strtotime() and date()
              > functions in combination to generate from MySQL into a readable format.
              > ...
              ><?
              > print ( "<br> strtotime for '0000-00-00 00:00:00' : " . strtotime (
              > '0000-00-00 00:00:00' ) );
              > print ( "<br> date for the above : " . date ( "d-m-Y", strtotime (
              > '0000-00-00 00:00:00' ) ) );
              > ?>
              > [/snippet]
              >
              > I am on PHP 5.0.2
              > Why this unusual behaviour? Or is it an expected behaviour?[/color]

              It's good to understand how things work (or not).

              Consider your case: strtotime("0000-00-00 00:00:00")

              The year 0000 is taken as 2000, due to Y2K handling. (Years 00-68 are
              assumed to be 2000-2068 and 69-99 are 1969-1999.) Arguably, it should
              be distinguishing 00 from 0000, but it doesn't.

              January is month 1, so "month 0" is one month before that, December.

              Similarly "day 0" is one day before the first of the month: the last day of
              the previous month.

              Therefore 0000-00-00 is one day before one month before 2000-01-01,
              giving 1999-11-30.

              So it makes sense, even if it is fundamentally wrong!

              Comment

              • Stefan Rybacki

                #8
                Re: unusual behaviour of strtotime() for '0000-00-00 00:00:00' input

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                ljb schrieb:[color=blue]
                > rithish@gmail.c om wrote:
                > ...
                >
                > Therefore 0000-00-00 is one day before one month before 2000-01-01,
                > giving 1999-11-30.
                >
                > So it makes sense, even if it is fundamentally wrong![/color]

                Its just math ;)
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.2.1 (MingW32)

                iD8DBQFESZNgyeC Lzp/JKjARAqwCAJwI9N JEQ1RIWpiHHzDSU BcHTZYLjgCeKtoU
                M5+ltJrhWMcLTra CCiXJHyY=
                =TCwa
                -----END PGP SIGNATURE-----

                Comment

                Working...