问题
Alright so I have this code which makes a list of dates:
$dates = array();
for($i = 1; $i < 10; $i++)
{
$datetime = mktime(12, 0, 0, date('n'), date('j') + $i, date('Y'));
if(date('N', $datetime) != 6 && date('N', $datetime) != 7)
{
$dates[date('l, F jS', $datetime)] = date('l, F jS', $datetime);
}
}
The dates are tomorrow and on as long as they are not Saturday or Sunday.
Now the problem is "tomorrow" is being changed at like 8:00 PM EST.
To explain, say it's Wednesday. The first option in the list should be Thursday. However, once it is 8:00 PM EST, then the first option is Friday. Instead of changing at 8:00 PM EST I'd like it to change at 3:00 AM EST (so on Thursday @ 2:00 AM it should still offer Thursday as a choice)
回答1:
I think you are getting your time zones confused currently. 8PM EDT = Midnight UTC.
Anyway, in PHP 5.3:
$one_day = new DateInterval('P1D');
$tz = new DateTimeZone('America/New_York');
$start = new DateTime('now', $tz);
if ($start->format('G') >= 3)
$start->add($one_day);
foreach (new DatePeriod($start, $one_day, 10) as $date)
{
if ($date->format('N') < 6)
echo $date->format('l, F jS')."\n";
}
The logic remains the same if you are on < 5.3, just have to do things with the other functions like strtotime(), date(), etc:
$one_day = 86400;
date_default_timezone_set('America/New_York');
$start = time();
if (date('G', $start) >= 3)
$start += $one_day;
for ($i = 0, $date = $start; $i < 10; ++$i, $date += $one_day)
{
if (date('N', $date) < 6)
echo date('l, F jS', $date)."\n";
}
I think that should work.
来源:https://stackoverflow.com/questions/3878790/php-adjusting-for-timezones