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.
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
Related
Sorry if this is not the correct place to post this but doing research on Google I haven't been able to find a suitable answer.
What I want to achieve is a user types in a date and time on the site and this gets inserted into the database (two different fields, a date field and a time field).
I then need to get the total of minutes until that day/time. I have the following code which prints out the days, hours and minutes. But I'm unsure of how to convert it to a total of minutes:
date_default_timezone_set("Europe/London");
$now = new DateTime();
$future_date = new DateTime(''.$Date.''.$Time.'');
$interval = $future_date->diff($now);
echo $interval->format("%a days, %h hours & %i minutes");
$now = new DateTime();
$future_date = new DateTime($Date.' '.$Time);
$minutes = ($future_date->getTimestamp() - $now->getTimestamp())/60;
Unix timestamp stores the date in seconds. So you can calculate it without initialize a DateInterval with the diff() function.
We have an entry in database for an event time left as P1DT3H53M45S, This means 1 day, 3 hours 53 min and 45 sec. I wan to retrieve date from this format.
I can retrieve the duration left by exploding this string and the calculate and then add to current time and create date.
Is there a better way to find the duration left other than exploding ?
No need for explode. Use DateInterval instead:
$interval = new DateInterval('P1DT3H53M45S');
echo $interval->format('%d day, %h hours, %I minutes, %s seconds');
// 1 day, 3 hours, 53 minutes, 45 seconds
P1DT3H53M45S is not date, but interval. You can use DateInterval class to create a DateInterval object, from which you can format it, or add it to some of your date.
$interval = new DateInterval('P1DT3H53M45S');
print_r($interval);
# format it
echo $interval->format('%d days, %h hours, %i minutes, %s seconds');
# add it to some date
$dt = new DateTime;
$dt->add($interval);
echo $dt->format('Y-m-d H:i:s');
demo
I'm calculating a timedifference with this bit of php and formatting it in days, hours and minutes.
// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
$enddate = $interval->format("%a days, %h hours, %i minutes");
// if current time is higher than expiration date set contest to finished.
if($now > $future_date) {
$enddate = 'Date ended';
}
Now I want to have the format to only display the total amount of days when it's over a day(24 hours) and start to format in hours and minutes when it's less than a day(24 hours). So it will format to hours and minutes starting from like 23 hours 59 minutes, you get the idea hopefully.
Can anyone tell me how does it done the easiest?
Do it like this:
// Compares expires_at with the current time
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
if ($now > $future_date) {
// if current time is higher than expiration date set contest to finished.
$enddate = 'Date ended';
} else if ($interval >= new DateInterval("P1D")) {
$enddate = $interval->format("%a days, %h hours, %i minutes");
} else {
$enddate = $interval->format("%a days, %h hours, %i minutes");
}
I'm calculating the difference between 2 dates using DateTime() and it's working fine. The problem is that I want to have the days format be able to go above a full month so 30/31 or higher.
$now = new DateTime();
$future_date = new DateTime($contest->expires_at);
$interval = $future_date->diff($now);
$enddate = $interval->format("%m month, %d days, %h hours, %i minutes");
The current problem with this is that when I don't display the months, the days can only go up to 30/31 and anything over that will be carried over to make a new month resetting the days count with the leftover days.
I want to be able to display 42 days when the difference is 6 weeks with this kind of format:
$enddate = $interval->format("%d days, %h hours, %i minutes");
Is there a quick fix for this or do I need to manually convert the timestamp to seconds and use my own function with modulus operators for this?
You can try:
$enddate = $interval->format("%a days, %h hours, %i minutes");
See the DateInterval::format in the manual.
NOTE
Take care of the bug if you're working on windows.
This should solve your porblem:
$now = new DateTime();
$future_date = new DateTime();
// a period of 2 months
$addPeriod = new DateInterval('P2M');
// adding the period
$future_date->add($addPeriod);
// get the differnce
$interval = $future_date->diff($now);
echo($interval->days) . ' days';
For today: echo returns '61 days'
// EDIT
To avoid running into the dataInterval-Bug you can use:
$now = new DateTime();
$future_date = new DateTime();
// a period of 2 months
$addPeriod = new DateInterval('P2M');
// adding the period
$future_date->add($addPeriod);
// get the difference in second
$diffTimestamp = $future_date->getTimestamp() - $now->getTimestamp();
// convert to days
// 1 day = 86.400 seconds
$diffDays = $diffTimestamp/86400;
echo(floor($diffDays)) . ' days';
Update my php version since this is a bug in my old and it works perfectly now.
How to get aggregate days from PHP's DateTime::diff?
My whole site is:
date_default_timezone_set('Europe/London');
A user can select their timezone, and in the database it saves as 4.0 (for Dubai)
Now I wish to have so the timezone the user has chosen has an impact on the countdown, that exists on the site. The countdown looks like this:
$today_date = new DateTime('now'); // now
$final_date = new DateTime($date); // a date in the future
$interval = $today_date->diff($final_date); // find the difference
$time_left = $interval->format('starting in %d days, %h hours and %i minutes'); // display it
Above this, i did following:
$userGMT = $user->get_gmt();
date_default_timezone_set($userGMT);
Which does not work correct. It still counts down for Europe/London timezone, and not the one I have chosen.
I tried do echo date('H:i:s'); and can see by the time that the above has affected and it is showing the time for the timezone I have picked.
So that works, but dateTime('now'); doesnt?
What you want to do is add the $userGMT to your currentTime and then subtract your server time. So get the time in seconds then add $userGMT*60*60 - $yourTimeZone*60*60
When the date in the future is generated, it needs to be explicit about the applicable timezone. This demonstrates that the interval is independent of the host's timezone:
<?php
$date = '2012-04-19'; // date in the future
$user_tz = new DateTimeZone('Asia/Dubai');
foreach (array('Europe/London', 'America/Los_Angeles') as $tzname)
{
date_default_timezone_set($tzname);
$today_date = new DateTime('now'); // now
$final_date = new DateTime($date, $user_tz); // future date with explicit tz
$interval = $today_date->diff($final_date); // find the difference
$time_left = $interval->format('start in %d days, %h hours and %i minutes');
echo "$tzname: $time_left\n";
}
Output:
Europe/London: start in 0 days, 11 hours and 53 minutes
America/Los_Angeles: start in 0 days, 11 hours and 53 minutes