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
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.
I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)
function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($dateP);
echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
if ($comp > "15 minutes ?") {
return true;
}
}
You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:
$difference = $start_date->diff($date2);
if($difference->i > 15) {
echo "difference greater than 15 minutes"
}
A date interval stores either a fixed amount of time (in years,
months, days, hours etc) or a relative time string in the format that
DateTime's constructor supports.
I am trying to find the time difference between an expiry date and today's date, but my code doesn't work.
$time_one=DateTime::createFromFormat('d/m/Y', get_field('expirydate'));
$time_two=new DateTime();
$timeleft = $time_one->diff($time_two);
echo $timeleft;
Try:
echo $timeleft->format('%a days');
So when you apply diff function to DateTime object -> you receive a DateInterval object. To access its properties about total difference between dates you should apply method called format.
The diff method returns you an DateInterval object. You must format the output in order to see how many days, hours, minutes, seconds remained:
$dateTimeInTheFuture = DateTime::createFromFormat('d/m/Y', get_field('expirydate'));
$dateInterval = $dateTimeInTheFuture->diff(new DateTime());
echo 'Time remained until expire: ' . $dateInterval->format('%d days, %h hours, %i minutes, %s seconds');
Read more about DateInterval.format method
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
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