1

I have a $time variable set some time in the future. In this format 2011-02-15 23:19:44. Now I need to set time remaining in this format 00:00:10.

If for example the $time is 1 hour, 3 minutes and 10 sec in the future. The format would be 01:03:10

ganjan
  • 7,356
  • 24
  • 82
  • 133

2 Answers2

1

You should look at using the DateTime and DateInterval classes.

$futureDate = new DateTime($time);
$currentDate = new DateTime();

$dateDiff = $currentDate->diff($futureDate);

echo $dateDiff->format('%H:%I:%S');

Note: if the difference is more than 24 hours, then you should look at the %Y-%m-%d values or %a for total days.

If you wanted to show any additional days as multiples of 24 hours then:

printf('%d:%s', 
    $dateDiff->format('%a') * 24 + $dateDiff->format('%H'), 
    $dateDiff->format('%I:%S')
);
Jacob
  • 8,278
  • 1
  • 23
  • 29
0

This is essentially the same problem as discussed here: PHP Time Since Function?

You're wanting to compare a future timestamp against the current time... the solutions in the linked question will also apply to this one.

Community
  • 1
  • 1
Chris Baker
  • 49,926
  • 12
  • 96
  • 115