<?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;
}
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 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 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