Best way to find out if the datetime string returned from MySQL is empty?

A datetime field that is not set in MySQL will be returned as 0000-00-00 00:00:00

What is the best way to determine if a date is set in PHP?

Not if they work:

if($date) //will return true because it is a valid string if(empty($date)) //will also return true. if(strtotime($date)) //I thought this would work but it returns a negative number and is true. 

I could use:

 if($date == '0000-00-00 00:00:00') 

However, if I ever changed the column type to Date , it will break. Therefore, I do not like it either.

I was looking for something like is_date , but all I could find was a function called checkdate , which doesn't seem to be what I'm looking for.

I suppose I could use:

 if(strtotime($date) > 0) 

I do not see any obvious problems with this, but I still thought there might be something better.

What is the standard way?

+4
source share
2 answers
 $dbDate = '0000-00-00 00:00:00'; if (strtotime($dbDate) == 0){ echo 'Empty date'; } 

http://www.ideone.com/eNffC

+5
source
 if ((int)$dbDate) echo 'Date is set'; 

for a solution based on a conversion of the unix era, some time zone problems may arise.

+1
source

All Articles