1

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:

PHP strtotime() outputs nothing

How come?

Thanks.

2
  • 3
    Which version of php do you use? Are you absolutely positive that you're passing '0000-00-00 00:00:00' to strtotime() and not a default value the (my?)sql server put into the table instead of 0000-... ?
    – VolkerK
    Jan 12, 2010 at 15:05
  • 1
    As a side note, that's the UNIX Time value for Mon, 11 Jan 2010 21:57:38 GMT
    – Powerlord
    Jan 12, 2010 at 15:11

5 Answers 5

3

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.

1

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

1

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);
}
0

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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.