Zack
Zack

Reputation: 183

Add PHP Time Diifference Value

How to add times similar to below scenario.

$start1 = date_create('05:16');
$end1 = date_create('08:48');
$diff1=date_diff($end1,$start1);
$time1=$diff1->format("%h Hours %i mintues");
echo $time1; //3 Hours 32 mintues

$start2 = date_create('07:05');
$end2 = date_create('08:37');
$diff2=date_diff($end2,$start2);
$time2=$diff2->format("%h Hours %i mintues");
echo $time2; //1 Hours 32 mintues

How to add both times i-e $total= $time1+$time2 which gives 5 hours 4 minutes

Upvotes: 1

Views: 21

Answers (1)

Abanoub Makram
Abanoub Makram

Reputation: 461

you can do like this:

$start1 = date_create('05:16');
$end1 = date_create('08:48');
$diff1=date_diff($end1,$start1);
$time1=$diff1->format("%h:%i");
echo $time1; //3 Hours 32 mintues

$start2 = date_create('05:16');
$end2 = date_create('08:48');
$diff2=date_diff($end2,$start2);
$time2=$diff2->format("%h:%i");
echo $time2; //1 Hours 32 mintues

$secs = strtotime($time1)-strtotime("00:00:00");
$hours = date("H",strtotime($time2)+$secs);
$minutes = date("i",strtotime($time2)+$secs);
$final_time=$hours." Hours ".$minutes." Minutes";
echo $final_time;

Upvotes: 1

Related Questions