开发者

format datetime(0000-00-00 00:00:00 ) to 00:00:00 with time remaing

开发者 https://www.devze.com 2023-02-11 10:49 出处:网络
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 ti开发者_开发问答me remaining in this format 00:00:10.

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 ti开发者_开发问答me 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


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')
);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号