php - 调整时区 [英] php - adjusting for timezones

查看:169
本文介绍了php - 调整时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有这个代码列出了日期:

  $ dates = array(); ($ 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 [ l,F jS',$ datetime)] = date('l,F jS',$ datetime);
}
}

日期是明天,只要他们是不是星期六或星期天。



现在问题是明天正在美东时间晚上8:00进行更改。



说这个星期三。列表中的第一个选项应为星期四。但是,一旦是美国东部时间下午8:00,那么第一个选择是星期五。而不是在美国东部时间晚上8:00改变,我希望在美国东部时间上午3:00更改(所以星期四@ 2:00 AM,它应该仍然提供星期四作为选择)

解决方案

我想你现在正在让你的时区感到困惑。 8PM EDT = Midnight UTC。



无论如何,在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')。\\\
;
}

5.3,只需执行其他功能,例如 strtotime() date()等: p>

  $ one_day = 86400; 
date_default_timezone_set('America / New_York');
$ start = time();

if(date('G',$ start)> = 3)
$ start + = $ one_day;

($ i = 0,$ date = $ start; $ i< 10; ++ $ i,$ date + = $ one_day)
{
if date('N',$ date)< 6)
echo date('l,F jS',$ date)\\\
;
}

我认为应该工作。


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)

解决方案

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.

这篇关于php - 调整时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆