-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
  • 959
  • 3
  • 13
  • 29

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
  • 6,650
  • 2
  • 16
  • 36
Saeed M.
  • 2,088
  • 2
  • 23
  • 45
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
  • 2,008
  • 21
  • 33
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,110
  • 1
  • 18
  • 35
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
  • 564
  • 1
  • 7
  • 25