470,596 Members | 1,564 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

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.

Apr 21 '06 #1
7 5174
Rithish wrote:
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. From http://dev.mysql.com/doc/refman/5.1/en/datetime.html:

"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

Apr 21 '06 #2
Oli Filth wrote:
0000-00-00 is not in the supported range of times in MySQL.


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

Regards,
Rithish.

Apr 21 '06 #3
El 21 Apr 2006 08:30:26 -0700
Rithish escribió:
Hello. I noticed a strange thing while using strtotime() and date()
functions in combination to generate from MySQL into a readable
format.


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

SELECT date_format('0000-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 (ju****@iteisa.com)
ITEISA (http://www.iteisa.com)
942544036 - 637447953
Apr 21 '06 #4
Rithish said the following on 21/04/2006 16:44:
Oli Filth wrote:
0000-00-00 is not in the supported range of times in MySQL.


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


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
Apr 21 '06 #5
>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.
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.
[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?


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
Apr 21 '06 #6
ljb
ri*****@gmail.com wrote:
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?


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!
Apr 22 '06 #7
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ljb schrieb:
ri*****@gmail.com 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!


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

iD8DBQFESZNgyeCLzp/JKjARAqwCAJwI9NJEQ1RIWpiHHzDSUBcHTZYLjgCeKtoU
M5+ltJrhWMcLTraCCiXJHyY=
=TCwa
-----END PGP SIGNATURE-----
Apr 22 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

45 posts views Thread by Jordan Rastrick | last post: by
2 posts views Thread by Ken Tuck | last post: by
4 posts views Thread by Kevin O'Donovan | last post: by
3 posts views Thread by slonial | last post: by
reply views Thread by amrhi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.