1

I am insert a two time in database and how to find the difference to two times the result is show like h:s:m format i will try to find the difference of this time

$from_time = $worker->timefm; //08:30:00  
$to_time= $worker->timeto;  //17:00:00
$diff=$to_time-$from_time;
echo $diff; //9

The answer show 9 only ,the correct answer is 8:30 why this show only 9 how the answer show in time format how to solve this issue ? help me!! thanks in advance

i will try this codes

How to get time difference in minutes in PHP

but not work

Andrew
  • 840
  • 3
  • 17
  • 43
  • 4
    Possible duplicate of [How to get time difference in minutes in PHP](https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – Ali Aug 13 '18 at 05:43

3 Answers3

0

Here I'm using date() function. Before calling this this function, I'm making the default time zone to UTC using date_default_timezone_set().

date_default_timezone_set('UTC');
$diff= strtotime($to_time) - strtotime($from_time);
echo date('H:i:s', $diff);

OUTPUT
08:30:00
Saral
  • 1,087
  • 1
  • 8
  • 18
0

You can use this function to calculate the time difference it will also work with the date and time data. It works simply by getting difference in seconds and then converting it into h:m:s format.

function get_time_difference($t1, $t2)
{
    $t1 = strtotime($t1);
    $t2 = strtotime($t2);;
    $hour = floor(($t1-$t2)/3600);
    $minute =floor(($t1-$t2)/60)%60;
    $second = ($t1-$t2)-($hour*3600+$minute*60); 
    return (date("H:i:s",strtotime($hour.":".$minute.":".$second)));
}
Anurag Phadnis
  • 817
  • 7
  • 13
0

<?php

// Time Calculation
$datetime1 = new DateTime('13:40:50');
$datetime2 = new DateTime('15:45:57');

$interval = $datetime1->diff($datetime2);
echo $interval->format('%H:%M:%S time');

?>

Just run this code and you will get the time difference in H:M:S format like this 02:00:07 time

Harunduet
  • 937
  • 10
  • 11