Monday, November 11, 2013

How To Check If 24 Hours Have Passed in PHP?

You have in php strtotime() function to convert human readable dates to timestamps. Using strtotime function you can easily compare dates or timestamp with php.

In the following example, I have converted the two dates into timestamp using strtotime function and stored in separate variables. You can do many things for instance, compare two dates to find out which date is greater or lower. I have to find out if the date have been more than 24 hours, so I did following method, if 24 hours have passed.

// your first date coming from a mysql database (date fields) 
$dateA = '2013-11-11 23:10:30'; 
// your second date coming from a mysql database (date fields) 
$dateB = '2013-11-11 16:27:21'; 

$timediff = strtotime($dateA) - strtotime($dateB);

if($timediff > 86400){ 
    echo 'more than 24 hours';
 echo 'less than 24 hours';

If it exists other ways to achieve it, please share it. :)

1 comment: