简体   繁体   中英

difference between two dates in php

I have these 2 dates stored in mysql :

2012-10-05
2012-10-10

I got the dates returned using php/mysql now I need to find out how many days separate them. in this example it would be 5days. any suggestion of what would be the best way to do it ?

SELECT DATEDIFF(date1, date2)
FROM yourtable

as per the MySQL docs: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff

datediff() returns the difference of date1 - date2 in days.

$date1 = strtotime("2012-10-05");
$date2 = strtotime("2012-10-10");



$days = floor(abs($date2 - $date1)/ (60*60*24));

printf("%d days",$days);
$date1 = new DateTime('2012-10-05');
$date2 = new Datetime('2012-10-10');
$interval = $date1->diff($date2);
echo $interval->format('%R%d days');  // +5 days

Try something like this :

$date1 = "2007-03-24"; $date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\\n", $years, $months, $days);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM