获取特定时区的当前PHP日期时间
问题真的很简单,但我不知道为什么事情不能以一种简单的方式进行 我想知道特定时区的日期时间。我的服务器的时区是获取特定时区的当前PHP日期时间,php,date,datetime,Php,Date,Datetime,问题真的很简单,但我不知道为什么事情不能以一种简单的方式进行 我想知道特定时区的日期时间。我的服务器的时区是America/Chicago,但我想获取不同时区的当前日期时间 我不想date\u default\u timezone\u set,因为它会更改所有datetime函数的时区 我尝试了$now=newdatetime(null,newdatetimezone('Europe/Stockholm')具有不同的时区值,但都返回相同的值 考虑以下代码 $current_time = date
America/Chicago
,但我想获取不同时区的当前日期时间
date\u default\u timezone\u set
,因为它会更改所有datetime函数的时区$now=newdatetime(null,newdatetimezone('Europe/Stockholm')代码>具有不同的时区值,但都返回相同的值
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
$now = new DateTime(null, new DateTimeZone('America/New_York'));
echo "America/New_York = ". $now->getTimestamp() . "\r\n";
$now = new DateTime(null, new DateTimeZone('Europe/Stockholm'));
echo "Europe/Stockholm = ". $now->getTimestamp() . "\r\n";
$now = new DateTime(null, new DateTimeZone('Asia/Muscat'));
echo "Asia/Muscat = ".$now->getTimestamp() . "\r\n";
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
上述代码产生以下输出
The current server time is: 2015-04-04 17:06:01
America/New_York = 1428185161
Europe/Stockholm = 1428185161
Asia/Muscat = 1428185161
The current server time is: 2015-04-04 17:06:01
这三个值都是相同的,意味着新的日期时区(XYZ)不起作用。预期/要求的输出应反映这些特定时区的当前时间
如果我遗漏了什么,请给出建议。Unix时间戳总是以UTC为单位,因此总是相同的。尝试使用
c
格式化程序查看差异:
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
$now = new DateTime(null, new DateTimeZone('America/New_York'));
echo "America/New_York = ". $now->format('c') . "\r\n";
$now = new DateTime(null, new DateTimeZone('Europe/Stockholm'));
echo "Europe/Stockholm = ". $now->format('c') . "\r\n";
$now = new DateTime(null, new DateTimeZone('Asia/Muscat'));
echo "Asia/Muscat = ".$now->format('c') . "\r\n";
$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";
The current server time is: 2015-04-04 22:26:56
America/New_York = 2015-04-04T18:26:56-04:00
Europe/Stockholm = 2015-04-05T00:26:56+02:00
Asia/Muscat = 2015-04-05T02:26:56+04:00
The current server time is: 2015-04-04 22:26:56
所以唯一的区别是使用
$now->format()
而不是$now->getTimestamp()
。我尝试了Y-m-dh:I:s
格式,而不是c
,现在一切看起来都很好。答案被接受。谢谢,亲爱的。