谁能推荐一种简单的方法来将日期和时间转换为 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/