1

I have the following dates and I would like the difference in the format hh:mm:ss.

I have the following code,

$from = "2019-05-20 23:59";
$to = "2019-05-27 00:00";

$expiry_time = new DateTime($to);
$current_date = new DateTime($from);
$diff = $expiry_time->diff($current_date);
return $diff->format('%H:%I:%S');

The above returns 00:01:00, but I expect it to be 114:01:00, since its 6 days (6*24 = 114).

I have also tried

$from = "2019-05-20 23:59";
$to = "2019-05-27 00:00";
$time_diff = strtotime($to) - strtotime($from);
return date('H:i:s', $time_diff);

Which also gives 00:01:00

What am i missing? How can i convert this to show even beyond 24 hours? I have checked get time difference in hours minutes and seconds to no avail.

Qirel
  • 25,449
  • 7
  • 45
  • 62
Geoff
  • 6,277
  • 23
  • 87
  • 197
  • The diff is 6 days 1 hour, and with your code you get the hour part, which is 1 hour... It does not convert days to hours automatically – François Huppé Jul 20 '19 at 13:24

1 Answers1

2

You can access the days property of the $diff object, and multiply it by 24 (as there are 24 hours in a day), then add the H format (to get the remaining hours) before concatenating the minutes and seconds.

The H value will never exceed 24, as the hours in a day will never be above that.

$from = "2019-05-20 23:59";
$to = "2019-05-27 00:00";

$expiry_time = new DateTime($to);
$current_date = new DateTime($from);
$diff = $expiry_time->diff($current_date);
return ($diff->days*24 + $diff->format("%H")).$diff->format(':%I:%S'); // 144:01:00
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • How can I create an api for whatsapp like android app. Can you create a tutorial for that @Qirel – Pie Jul 21 '19 at 10:09
  • 1
    That's a very big task, and not at all related to the question here. @Pie – Qirel Jul 21 '19 at 12:45