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
Related
In my form there are 2 Time Pickers where user can select a from time and to time. It doesn't have a date associated with it. And for a report generating purpose I've to calculate the time difference between them. It works perfectly to if the From and to Time is "06:00 to 10:00" but if the from and to time is "21:00 to 02:00" I get a time difference of 19 hours. Could you please help me to fix this.
For this case "21:00 to 02:00" the time difference should be 5 hours.
This is the Code
$datetime1 = new \DateTime('09:30 PM');
$datetime2 = new \DateTime('02:00 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh');
exit;
The difference becomes negative If $totime is less than $fromtime.
DateInterval->invert == 1 indicates that. This is used with this short solution to correct the result.
$fromtime = '09:30 PM';
$totime = '02:00 AM';
$diff = date_create($fromtime)->diff(date_create($totime));
$hours = $diff->invert ? 24-$diff->h : $diff->h;
echo $hours; //5
Since you have 2 date pickers one for from time and another to time, the former will always be smaller than the latter. Hence when from time is larger than to time it means user has selected to from the next day. If we don't add a date for calculating difference, PHP will assume today's date by default. We can easily fix this by adding a condition to compare the times and prepend the dates accordingly. Below is the updated code.
<?php
$fromtime = '09:30 PM';
$totime = '02:00 AM';
$now = new \DateTime();
$today = $now->format('Y-m-d'); // Store current date
$now->add(new DateInterval('P1D')); // Add one day to current date to get next date
$nextDay = $now->format('Y-m-d'); // Store next date
if($fromtime > $totime) // If from time is bigger than to time, it means to is a next day
{
$fromdatetime = "$today $fromtime";
$todatetime = "$nextDay $totime";
}
else
{
$fromdatetime = "$today $fromtime";
$todatetime = "$today $totime";
}
$datetime1 = new \DateTime($fromdatetime);
$datetime2 = new \DateTime($todatetime);
$interval = $datetime1->diff($datetime2);
echo $interval->format('%hh');
?>
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.
<?php
date_default_timezone_set('America/New_York');
$current_time = time();
$now = new DateTime();
$b = $current_time;
$future_date = new DateTime('2011-05-11 09:30:00');
$interval = $future_date->diff($now);
echo $interval->format("%h hours, %i minutes, %s seconds");
?>
i want to show the the Market remaining time, Market open at 9:30 am, the above code is working fine but problem is $current_time is showing my system current time instead of Nerw york time, so its showing the remaining time by by considering my system time.i want to show the American current time so i can easily show the remaining time. Thank in Advance.
This I think is closer to what you need.
I have made sure that I set a specific timezone on both the DateTime objects so there can be no confusion
This also takes into account the possibility that the market could already be open, and tells you how long to closing time.
It also make use of Todays date in all cases, so any daylight saving time should be coped with.
There is also a time setter for the current time so you can check what happens at different times of the day, see $now->setTime(10, 30, 0); for setting a specific time of day.
<?php
$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/New_York'));
// To test what happens at different times of the current day
// Set a specific time for the NOW DateTIme
$now->setTime(10, 30, 0);
echo 'Now = ' . $now->format('d/m/Y H:i:s').PHP_EOL;
$opens = new DateTime();
$opens->setTimezone(new DateTimeZone('America/New_York'));
$opens->setTime(9, 30, 0);
echo 'Opens = ' . $opens->format('d/m/Y H:i:s').PHP_EOL;
$interval = $now->diff($opens);
// if invert == 1 its a minus difference so market is open
if ( $interval->invert == 1){
// Its already open
echo $interval->format("Market has been open for: %h hours, %i minutes, %s seconds").PHP_EOL;
// When will it close
$close = new DateTime();
$close->setTimezone(new DateTimeZone('America/New_York'));
$close->setTime(3, 30, 0);
$interval = $now->diff($close);
echo $interval->format("Market closes in: %h hours, %i minutes, %s seconds").PHP_EOL;
} else {
// it will open in
echo $interval->format("Market opens in: %h hours, %i minutes, %s seconds").PHP_EOL;
}
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
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?