Show days / hours up to a specific time

I need to calculate the remaining time (days / hours) before a specific date / time.

However, I am not using a static date.

Imagine that I have an event at 17:00 on every Sunday . I need to display the remaining time until the next event, that is, the coming Sunday at 17:00.

I found the following code in the answer. It works for a static date / time, but obviously this is not what I am looking for.

$now = new DateTime();
$future_date = new DateTime('2011-05-11 12:00:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");

Thank you for your time.

+4
source share
1 answer

You can use a relative time format next Sunday 17:00. Like this:

$now = new DateTime();
$future_date = new DateTime('next Sunday 17:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");

Conclusion:

6 days, 2 hours, 33 minutes, 53 seconds

: http://www.php.net/manual/en/datetime.formats.relative.php

+9

Source: https://habr.com/ru/post/1540218/


All Articles