简体   繁体   English

DateTime-> format(“ U”)不返回带有时区信息的值

[英]DateTime->format(“U”) doesn't return a value with timezone info

In PHP 5.2, I'm using the following code to get a timestamp from a DateTime object. 在PHP 5.2中,我使用以下代码从DateTime对象获取时间戳。

$dateTime = new DateTime("now", new DateTimeZone("America/Los_Angeles") );

echo $dateTime->format("U");

the problem is that format("U") simply returns server timestamp, which is UTC. 问题是format(“ U”)仅返回服务器时间戳,即UTC。

How do I make it to return a timestamp from Pacific Time Zone (Los Angeles) ? 如何使其从太平洋时区(洛杉矶)返回时间戳?

您的时间戳概念不正确,时间戳与时区无关 ,它的定义是自1970年1月1日午夜协调世界时(UTC)起经过的秒数

Try setting timezone at top of PHP script. 尝试在PHP脚本顶部设置时区。 I think timestamps are always UTC. 我认为时间戳记始终是UTC。 Use date() function to format it into what you need. 使用date()函数将其格式化为所需的格式。

    // set timezone to pacific time
    date_default_timezone_set('America/Los_Angeles');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM