Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting dates in PHP [duplicate]

Possible Duplicate:
How to calculate the difference between two dates using PHP?

I have timestamps stored in the format YYYY-MM-DD HH:MM:SS (for example 2010-06-21 20:12:56). What would the best way to check how old the timestamp is? For the moment I am mainly interested in the number of days old.

like image 796
aslum Avatar asked Jun 22 '10 03:06

aslum


2 Answers

You can use strtotime to convert the string to a UNIX timestamp, which is in seconds. time() will give you the current UNIX timestamp. Subtract them to get how old the date is in seconds, and divide by 60*60*24 to get it in days

It's also doable using DateTime::diff, although I find the date functions easier than using the classes

like image 177
Michael Mrozek Avatar answered Oct 27 '22 02:10

Michael Mrozek


$today = strtotime(date('Y-m-d H:i:s'));
$expireDay = strtotime($row['ExpireDate']);
$timeToEnd = $expireDay - $today;
like image 36
Nguyen Avatar answered Oct 27 '22 03:10

Nguyen