views:

305

answers:

5

Hi!

I have a couple of blanks DATETIME values(0000-00-00 00:00:00) in my database.

Whenever i run those through strtotime, they return "1263247058" instead of FALSE, which it should do according to the info on the linked page:

http://stackoverflow.com/questions/1224680/php-strtotime-outputs-nothing

How come?

Thanks.

A: 

Works for me :\

$ php -r 'var_dump(strtotime("0000-00-00 00:00:00"));'
bool(false)
$

PHP 5.2.8 on mac OS, as well as PHP 5.2.5 on CentOS 5.2. Can you try the same test? If the result comes up different, what version of PHP are you using, and which platform?

FWIW, it strikes me that you could avoid this whole situation entirely by storing an actual NULL in the database instead of the zero-date.

Frank Farmer
+3  A: 

As one of the comments in the docs for strtotime says, "the value returned by strtotime("0000-00-00") varies by PHP version". So, it's better to do an additional check for that specific value.

Ignas R
A: 

Consider the output of the following code (PHP Version 5.3.0):

echo date( "h:i:sa m/d/Y", strtotime( '00:00:00' ) ) . "<br/>" ;
echo date( "h:i:sa m/d/Y", strtotime( '00:00:00 00-00-0000' ) ). "<br/>"  ;

// Output
12:00:00am 01/12/2010
12:00:00am 01/01/1970
St.Woland
+1  A: 

How come is explained in the other answers. This code will force the value to false:

if ($db_datetime == 0) {
    $unix_timestamp = false;
} else {
    $unix_timestamp = strtotime($db_datetime);
}
GZipp
A: 

Many PHP functions rely internally on the standard C library implementation and it's likely that this is the case of strtotime(). That means that the exact behaviour depends on the operating system PHP is running on.

I think it's better not to rely on strtotime() to detect invalid dates.

As a side note... I presume you are using MySQL. It's possible to configure it so it won't accept invalid dates:

NO_ZERO_DATE

Álvaro G. Vicario