4

I want to subtract one date to another using php and display result in the days - hours - min - sec format. How i do this using php . I tried this using time stamp but it does not give proper values. please give suggestion

For ex : from date 2012-04-27 19:30:56 to date 2012-04-27 19:37:56

i used this code

if(strtotime($history['datetimestamp']) > strtotime($lasttime)) {

                 $totalelapsed1 = (strtotime($history['datetimestamp'])-strtotime($lasttime)); 




                     if($totalelapsed1 > 60 ) {
                         $sec = $totalelapsed1%60;
                         $min = round($totalelapsed1/60 , 0);

                         $minute = $min + $minute;
                         $second = $sec + $second;

                        // echo $min. " min " .$sec." sec";

                     } else {
                         //echo "0 min " . $totalelapsed1." sec";

                         $minute = 0 + $minute;
                         $second = $totalelapsed1 + $second;

                    }



                } else {
                     $minute = 0 + $minute;
                     $second = 0 + $second;
                    // echo "0 min 0 sec";
                }
meenakshi
  • 87
  • 1
  • 8
  • [Check the answered post in this thread][1] [1]: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – ilight May 22 '12 at 10:31

4 Answers4

10

From how to subtract two dates and times to get difference by VolkerK:

You have to use like this:-

<?php

//$now = new DateTime(); // current date/time
$now = new DateTime("2010-07-28 01:11:50");
$ref = new DateTime("2010-07-30 05:56:40");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
prints 2 days, 4 hours, 44 minutes

see http://docs.php.net/datetime.diff

edit: But you could also shift the problem more to the database side, e.g. by storing the expiration date/time in the table and then do a query like ... WHERE key='7gedufgweufg' AND expires<Now() Many rdbms have reasonable/good support for date/time arithmetic.

Link Url:- how to subtract two dates and times to get difference

http://www.webpronews.com/calculating-the-difference-between-two-dates-using-php-2005-11

Community
  • 1
  • 1
Javascript Coder
  • 5,691
  • 8
  • 52
  • 98
5

I suggest to use DateTime and DateInterval objects.

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "days difference ".$interval->d." days "; 

read more php DateTime::diff manual

Andrew Hall
  • 3,058
  • 21
  • 30
0

Try using DateTime:diff().

Examples are provided on that page.

Otar
  • 2,561
  • 1
  • 20
  • 24
0

This script resolve my issue

$date1 = date("d-m-Y H:i:s",strtotime($date1));

$date2 = date("d-m-Y H:i:s",strtotime($lasttime));

$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);
Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
meenakshi
  • 87
  • 1
  • 8