php - php中的时区转换

标签 php datetime timezone timestamp

谁能推荐一种简单的方法来将日期和时间转换为 php 中的不同时区?

最佳答案

您可以为此使用日期时间对象或其函数别名:

Example (abridged from PHP Manual)

date_default_timezone_set('Europe/London');

$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');

编辑评论

but i cannt use this method because i need to show date in different time zones as the user login from different locations

这不是问题。当用户登录时,您确定他的时区并将其设置为您的 DateTime 对象,如图所示。我在我的一个项目中使用了类似的方法,它就像一个魅力。

in the database i need to get the dates in any single timezone, then only it can be processed properly

您可以将时间存储为一个时区中的时间戳或日期时间。当您查询 DateTime 字段时,您可以将 DateTime 对象中的时间转换为该时区,或者 - 如果您的数据库支持 - 使用所选时区进行查询。

关于php - php中的时区转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2505681/

相关文章:

php - 我正在访问日期为 : how to assign a value when a particular date is not present? 的记录

php - PHP 中的 Europe/London 和 UTC 之间有区别吗?

python - 计算一年中的周数(处理一年中的第一周)

python - Pandas.to_datetime 函数静默失败

python pandas日期时间转换为日期

Django 模型 __unicode__ : How to return a value containing localized datetimes?

mysql - Nodejs和mysql数据库时区问题

php - ${ } 在 PHP 语法中是什么意思?

php - 编写一个 php 文件来更新网页中的 HTML

php - 使用 php、mysql 和查询自动完成