可能重复:
How to calculate the difference between two dates using PHP?
我希望得到改变:这两个日期的天,小时,分钟和秒
php中的 2011-08-17 15:23:24
和2011-08-11 14:00:11
。
你能帮助我吗? 提前谢谢。
答案 0 :(得分:0)
<?php
$date1 = new DateTime('2011-04-01');
$date2 = new DateTime("now");
$interval = $date1->diff($date2);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
if($years!=0){
$ago = $years.' year(s) ago';
}else{
$ago = ($months == 0 ? $days.' day(s) ago' : $months.' month(s) ago');
}
echo $ago;
?>
答案 1 :(得分:0)
$d1=DateTime::createFromFormat('Y-m-d H:i:s','date1');
$d2=DateTime::createFromFormat('Y-m-d H:i:s','date2');
$interval=$d1->diff($d2);
//use $interval->format(); here
答案 2 :(得分:0)
$date1 = "2008-11-01 22:45:00";
$date2 = "2009-12-04 13:44:01";
$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));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minuts = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
printf("%d years, %d months, %d days, %d hours, %d minuts\n, %d seconds\n", $years, $months, $days, $hours, $minuts, $seconds);
khaldonno在此链接上回答How to calculate the difference between two dates using PHP?