Milan Malani
Milan Malani

Reputation: 1912

Is there any function to find difference between dates and time to date-time in php?

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

Upvotes: 1

Views: 410

Answers (4)

Milan Malani
Milan Malani

Reputation: 1912

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;

Upvotes: 0

vascowhite
vascowhite

Reputation: 18440

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

Upvotes: 2

Andy94
Andy94

Reputation: 56

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

Upvotes: 4

The Marlboro Man
The Marlboro Man

Reputation: 971

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.

Upvotes: -1

Related Questions