0
votes

I just edit my question I have have two time format i want the difference between them

For example

 $time1 = new DateTime('09:00:59');
 $time2 = new DateTime('100:30:00');
 $interval = $time1->diff($time2);
   echo $interval->format('%h:%i:%s second(s)'); 
  ?>

Its working fine below 24 hour showing me fatal error if i am increasing time2

$time2 = new DateTime('100:30:00');

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (100:30:00) at position 0 (1): Unexpected character' in D:\xampp\htdocs\datetime.php:3 Stack trace: #0 D:\xampp\htdocs\datetime.php(3): DateTime->__construct('100:30:00') #1 {main} thrown in D:\xampp\htdocs\datetime.php on line 3

Is there any other way or i can edit the same I have try a lot but not find out the soloution I just want the diffrence using any method Thanks

3
Can you post your related code? - you've tagged strtotime but that is for timestamps, so 24 would be the cap as that is 12 midnight So you would need to include days?Aravona
You have to include the dates, you can subtract a timestamp from a timestamp without any conversion as long as you use the full timestamp so that php can determine the numerical value based on the UNIX epoc 1/1/1970.VikingBlooded

3 Answers

2
votes

One way to do it:

$time2 = '100:00:00';
$time1 = '10:30:00';

list($hours, $minutes, $seconds) = explode(':', $time2);
$interval2 = $hours*3600 + $minutes*60 + $seconds;

list($hours, $minutes, $seconds) = explode(':', $time1);
$interval1 = $hours*3600 + $minutes*60 + $seconds;

$diff = $interval2 - $interval1;

echo floor($diff / 3600) . ':' . 
     str_pad(floor($diff / 60) % 60, 2, '0') . ':' . 
     str_pad($diff % 60, 2, '0');

Output:

89:30:00

Here is Codepad demo

1
votes

I Hope this may help.

$time1 = '10:30:00';
$time2 = '100:00:00';


function hms2sec ($hms) {
    list($h, $m, $s) = explode (":", $hms);
    $seconds = 0;
    $seconds += (intval($h) * 3600);
    $seconds += (intval($m) * 60);
    $seconds += (intval($s));
    return $seconds;
}
$ts1=hms2sec($time2);
$ts2=hms2sec($time1);
$time_diff = $ts1-$ts2;

function seconds($seconds) {

        // CONVERT TO HH:MM:SS
        $hours = floor($seconds/3600);
        $remainder_1 = ($seconds % 3600);
        $minutes = floor($remainder_1 / 60);
        $seconds = ($remainder_1 % 60);

        // PREP THE VALUES
        if(strlen($hours) == 1) {
            $hours = "0".$hours;
        }

        if(strlen($minutes) == 1) {
            $minutes = "0".$minutes;
        }

        if(strlen($seconds) == 1) {
            $seconds = "0".$seconds;
        }

        return $hours.":".$minutes.":".$seconds;

        }
echo $final_diff=seconds($time_diff);
0
votes

Since I don't have enough "reputation" to add a comment to the selected answer. I want to add a message to point out to a flaw with it.

If you try it with these arguments:

$time2 = '10:15:00';
$time1 = '10:10:00';

You will get an incorrect result of: 0:50:00

To correct this, you need to add STR_PAD_LEFT within the str_pad that deals with the minute as in:

str_pad(floor($diff / 60) % 60, 2, '0', STR_PAD_LEFT) . ':' .