-1

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

I want to get difference: days, hour, min and seconds from these two dates

2011-08-17 15:23:24 and 2011-08-11 14:00:11 in php.

Anyone can you help me? Thanks in advance.

Community
  • 1
  • 1
rajan
  • 1
  • 1
    http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php - Please check that the question hasn't been asked before. – Alex Aug 17 '11 at 10:54
  • Check http://php.net/manual/de/datetime.diff.php or http://www.google.de/search?hl=de&q=php+difference+between+two+dates – Olaf Aug 17 '11 at 10:55

3 Answers3

0

from example in 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; 
?> 
Ruslan
  • 9,927
  • 15
  • 55
  • 89
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
RiaD
  • 46,822
  • 11
  • 79
  • 123
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); 

Answered by khaldonno on this link How to calculate the difference between two dates using PHP?

Community
  • 1
  • 1
Rahul Kanodia
  • 105
  • 12