2011-08-17 44 views
-1

可能重复:
How to calculate the difference between two dates using PHP?如何获得最新diffrence

我想区别:天,小时,分钟和这两个日期

2011-08-17 15:23:242011-08-11 14:00:11秒在PHP中。

任何人都可以帮助我吗? 在此先感谢。

+1

http://stackoverflow.com/questions/676824/how-to-calculate-the回答khaldonno -difference-between-dates-using-php - 请检查以前没有问过这个问题。 – Alex

+0

查看http://php.net/manual/de/datetime.diff.php或http://www.google.de/search?hl=de&q=php+difference+between+two+dates – Olaf

回答

0

从例如在php manual

<?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; 
?> 
+0

小时和分钟 – rajan

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 
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); 

此链接How to calculate the difference between two dates using PHP?