Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Days/Hours Until Certain Time

Tags:

php

datetime

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.

like image 722
mmmbaileys Avatar asked Jan 11 '23 15:01

mmmbaileys


1 Answers

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

like image 104
hek2mgl Avatar answered Jan 22 '23 16:01

hek2mgl