Industrial
Industrial

Reputation: 42798

PHP date issue - strtotime does not output what it's supposed to?

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.

Upvotes: 1

Views: 1931

Answers (5)

Frank Farmer
Frank Farmer

Reputation: 39386

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.

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146630

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

Upvotes: 1

GZipp
GZipp

Reputation: 5426

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

Upvotes: 1

St.Woland
St.Woland

Reputation: 5427

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

Upvotes: 0

Ignas R
Ignas R

Reputation: 3409

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.

Upvotes: 3

Related Questions