15

I have two date's

$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"

I want to calculate the difference and display it as follows

1 hour 32 minutes

alamin8031
  • 173
  • 1
  • 1
  • 6

2 Answers2

38

Make use of DateTime::diff of the DateTime Class

<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";

OUTPUT :

1 Hours 32 Minutes
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
15

Simply convert both dates to timestamp if dont want to do it in complex way... Something like this

$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);

$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;

and there you go...

Thank you...

Vedant Joshi
  • 173
  • 1
  • 6