-1

I need to find the different between 2 timestamp values ,how to calculate it

$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';

I need output time value as 1h 45m 

How to get desired output

vellai durai
  • 1,019
  • 3
  • 16
  • 38

4 Answers4

6
<?php
$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
$d = strtotime($time2) - strtotime($time1);
echo gmdate("g", $d),'h ',gmdate('i',$d),'m ';
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
Saeed M.
  • 2,216
  • 4
  • 23
  • 47
2
$date_a = new DateTime('2016-08-31T07:00:00');
$date_b = new DateTime('2016-08-31T08:45:00');

$interval = date_diff($date_a,$date_b);

echo $interval->format('%h Hours:%i Min:%s sec');

You Can test the code Here

Muhammad Younas
  • 1,543
  • 1
  • 22
  • 32
1

Use strtotime() to convert datetime in timestamps and get the difference between 2 times.

Now, Divide the difference into desire format. Use below code.

$time1 ='2016-08-31T07:00:00';
$time2 ='2016-08-31T08:45:00';
$diff = abs(strtotime($time1) - strtotime($time2));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); 
$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); 
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); 

echo $hours . "h " .$minuts."m";

Output

1h 45m

Live Demo : Click Here

RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

Use this code,

$datetime1 = strtotime("09:00");
$datetime2 = strtotime($items['duration']);
$interval  = abs($datetime2 - $datetime1);
$min   = round($interval / 60);
$d = floor ($min / 1440);
$h = floor (($min - $d * 1440) / 60);
$m = $min - ($d * 1440) - ($h * 60);
if (!$m) {
  $m = '00';
}
echo $h."h:".$m."m";
Mansoor H
  • 594
  • 1
  • 8
  • 25