Question

I need to calculate the remaining time (days/hours) until a certain date/time.

However, I'm not using a static date.

Imagine I have an event at 17:00 hrs on every Sunday. I need to display the time remaining until the next event, i.e. the oncoming Sunday 17:00.

I've found the following code in this answer. It works for a static date/time, but obviously isn't what I'm 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");

Thanks for your time.

Was it helpful?

Solution

You can use the 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");

Output:

6 days, 2 hours, 33 minutes, 53 seconds

Read more about relative time formats here: http://www.php.net/manual/en/datetime.formats.relative.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top