1

I want the diff between two date-time in php. I want diff in H:i:s format. Here is my code.

$start_date = 2013-08-13;
$end_date = 2013-08-23;
$start_time = 12:28:58;
$end_time = 13:16:45;

$h1 = substr("$start_time",0,-6);
$i1 = substr("$start_time",3,-3);
$s1 = substr("$start_time",6);
$h2 = substr("$end_time",0,-6);
$i2 = substr("$end_time",3,-3);
$s2 = substr("$end_time",6);

$m1 = substr("$start_date",5,-3);
$d1 = substr("$start_date",8);
$y1 = substr("$start_date",0,-6);
$m2 = substr("$end_date",5,-3);
$d2 = substr("$end_date",8);
$y2 = substr("$end_date",0,-6);

$r1=date("Y-m-d H:i:s",mktime($h1,$i1,$s1,$m1,$d1,$y1));
$r2=date("Y-m-d H:i:s",mktime($h2,$i2,$s2,$m2,$d2,$y2));
jxh
  • 69,070
  • 8
  • 110
  • 193
Milan Malani
  • 1,818
  • 1
  • 22
  • 34
  • 2
    Have you tried [`DateTime`](http://php.net/datetime)? – Ja͢ck Aug 23 '13 at 06:25
  • yup...I tried but not working. In datetime i use DateTime::diff but i returns the Fatal Error like this. Fatal error: Call to undefined function date_diff() in c:\wamp\www\website\module\1\Save.php on line 57 – Milan Malani Aug 23 '13 at 06:30
  • Perhaps there's no DateTime class in your php version. – The Marlboro Man Aug 23 '13 at 06:37
  • 1
    It looks like you're using a **very** old PHP version if you're getting that error. It was added in 5.3. Try updating your server's software first. PHP 5.2 reached its [end of life](http://php.net/releases/index.php) on 6 Jan 2011 – Mike Aug 23 '13 at 06:40

4 Answers4

4

Why are you performing all those operations when you can use strtotime() ? It is simpler: if you join dates with times with a space then you can use the aforementioned function to generate a timestamp. Then, you obtain the difference between the two just with a simple calculation.

Finally, you will format the resulting timestamp with date("H:i:s") (in your case).

Here's the code.

$start_date = "2013-08-13";
$end_date = "2013-08-23";
$start_time = "12:28:58";
$end_time = "13:16:45";

$start_date = $start_date." ".$start_time;
$end_date = $end_date." ".$end_time;

$start_date = strtotime($start_date);
$end_date = strtotime($end_date);

$difference = $end_date - $start_date;

echo date("H:i:s", $difference);
Andy94
  • 56
  • 3
  • 1
    One thing i want to say is that strtotime function returns only the time diff between both variable not including date even concatenated. If their is gap of more than 2 days then also it returns the time diff within 24hours only. And I want total time between two date in H:m:s. Can you plz help. – Milan Malani Aug 23 '13 at 12:54
  • That's true, my bad. You can then use a function like the one here http://stackoverflow.com/a/3172665/1487536 , passing $difference as a parameter. Then you will have the expected result. This happens because there are 86400 seconds in a day, if you have more you can have unexpected results (as noted in the accepted answer of the link I posted before). – Andy94 Aug 26 '13 at 04:38
  • 1
    Actually the problem comes when you use date function after getting the diff value. Till diff comes it's OK. After that whatever i used is given below in reply. – Milan Malani Aug 26 '13 at 12:03
2

DateTime is the right tool for this job:-

$start_date = '2013-08-13';
$end_date = '2013-08-23';
$start_time = '12:28:58';
$end_time = '13:16:45';

$start = new \DateTime($start_date . ' ' . $start_time);
$end = new \DateTime($end_date . ' ' . $end_time);

$diff = $start->diff($end, true);

$diff is an instance of DateInterval, so you can use DateInterval::format() to echo out the time:-

echo $diff->format("%H:%I:%S");
vascowhite
  • 18,120
  • 9
  • 61
  • 77
0

This is what i used. And its working. If any suggestion please tell. Thanks To all for your reply.

function ensure2Digit($number) {
    if($number < 10) 
    {
        $number = '0'.$number;
    }
    return $number;
    }


$start_date = "2013-08-13";
$end_date = "2013-08-23";
$start_time = "12:28:58";
$end_time = "13:16:45";

$start_date = $start_date." ".$start_time;
$end_date = $end_date." ".$end_time;

$start_date = strtotime($start_date);
$end_date = strtotime($end_date);

$difference = $end_date - $start_date;

$h = ensure2Digit(floor($difference / 3600));
$m = ensure2Digit(floor(($difference / 60) % 60));
$s = ensure2Digit($difference % 60);
$time_taken =  $h.":".$m.":".$s;
Milan Malani
  • 1,818
  • 1
  • 22
  • 34
-1

Just whipped up this:

function sec_diff($date_ini, $date_end, $time_ini='00:00:00', $time_end='00:00:00')
{
    $d_ini=explode('-', $date_ini);
    $h_ini=explode(':', $time_ini);
    $d_end=explode('-', $date_end);
    $h_end=explode(':', $time_end);

    $t_ini=mktime($h_ini[0], $h_ini[1], $h_ini[2], $d_ini[1], $d_ini[2], $d_ini[0]);
    $t_end=mktime($h_end[0], $h_end[1], $h_end[2], $d_end[1], $d_end[2], $d_end[0]);

    return $t_end-$t_ini;
}

It will get you the difference in seconds. You can easily operate on the result to get the desired format: result / 3600 will give you hours. The rest of that operation will give you spare seconds, that you can / 60 to get minutes. Any rest are seconds.

The Marlboro Man
  • 971
  • 7
  • 22