Code Kris
Code Kris

Reputation: 447

how to get the time gap more than 24 hours in php

i have to variable $a and $b $a= 645:00:00 and $b = 555:00:00 . i want to get the difference of the two time value .(both $a and $b are string value).how do is get out put as 90:00:00 using PHP

Already try code

  $totaltimeholiday = (strtotime($a) - strtotime($b));
  $hours = sprintf('%02d', intval($totaltimeholiday / 3600));
  $seconds_remain = ($totaltimeholiday - ($hours * 3600)); 
  $minutes = sprintf('%02d', intval($seconds_remain / 60));   
  $seconds = sprintf('%02d' ,($seconds_remain - ($minutes * 60)));
  $totaltimeholidayfinal = $hours.':'.$minutes.':'.$seconds;

but it return only 00:00:00

Upvotes: 0

Views: 115

Answers (1)

Hasitha Jayawardana
Hasitha Jayawardana

Reputation: 2436

Since your two times aren't valid times you can't use strtotime. But you can achieve what you need in a simple way as follows,

<?php

$a = "645:00:00"; 
$b = "555:00:00";

list ($hour1, $min1, $sec1) = explode(':', $a);
list ($hour2, $min2, $sec2) = explode(':', $b);

$sumHour = sprintf('%02d', $hour1 - $hour2);
$sumMin = sprintf('%02d', $min1 - $min2);
$sumSec = sprintf('%02d', $sec1 - $sec2);

echo $sumHour.':'.$sumMin.':'.$sumSec;

?>

The result is,

90:00:00

Upvotes: 1

Related Questions