有人能建议一个简单的方法来将日期和时间转换为PHP中的不同时区吗?
可以为此使用datetime对象或其函数别名:
示例(摘自PHP手册)
date_default_timezone_set('Europe/London');
$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') ."
";
$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
这不是问题。当用户登录时,您确定他的时区并将其设置为日期时间对象,如图所示。我在我的一个项目中使用了类似的方法,它就像一个魅力。
in the database i need to get the dates in any single timezone, then only it can be processed properly
您可以将时间存储为一个时区中的时间戳或日期时间。查询日期时间字段时,可以将日期时间对象中的时间转换为此时区,或者(如果数据库支持)使用所选时区进行查询。
感谢Gordon,但是我不能使用这个方法,因为我需要在不同的时区显示用户从不同地点登录的日期。
@Raki:所以在用户登录之后,用适当的用户选择的时区设置日期默认时区。
这也是不可能的。因为…在数据库中,我需要在任何一个时区中获取日期,然后才能正确地处理它。
在数据库中,你以格林威治标准时间存储所有东西。要么,要么,要么就变成了一个无法管理的混乱。
所有这些都比需要的要复杂。如果我设置了默认时区,那么我为获取创建/更新时间所做的日期("y-m-d h:i:s")位将在用户时区中。所以我必须把它们转换成UTC。到处都有很多变化,确保我不会两次进行转换。这实际上是一种阻抗失配。
更简单的方法如下:
date_default_timezone_set('Europe/London'); // your user's timezone
$my_datetime='2013-10-23 15:47:10';
echo date('Y-m-d H:i:s',strtotime("$my_datetime UTC"));
如PHP手册中所述,strtotime()也接受时区,只需将其附加到日期时间中即可。
我建议您将所有日期时间存储在UTC中,因为这样您就不会在夏令时遇到问题。
这对我很有用,而且也很干净!
function convert_to_user_date($date, $format = 'n/j/Y g:i A', $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC')
{
try {
$dateTime = new DateTime ($date, new DateTimeZone($serverTimeZone));
$dateTime->setTimezone(new DateTimeZone($userTimeZone));
return $dateTime->format($format);
} catch (Exception $e) {
return '';
}
}
function convert_to_server_date($date, $format = 'n/j/Y g:i A', $userTimeZone = 'America/Los_Angeles', $serverTimeZone = 'UTC')
{
try {
$dateTime = new DateTime ($date, new DateTimeZone($userTimeZone));
$dateTime->setTimezone(new DateTimeZone($serverTimeZone));
return $dateTime->format($format);
} catch (Exception $e) {
return '';
}
}
//example usage
$serverDate = $userDate = '2014-09-04 22:37:22';
echo convert_to_user_date($serverDate);
echo convert_to_server_date($userDate);
这些答案对我来说都不管用(我跳过了尝试过大的代码)。我还认为仅仅为了一次转换而更改默认时区是很奇怪的。
这是我的解决方案:
function changeTimeZone($dateString, $timeZoneSource = null, $timeZoneTarget = null)
{
if (empty($timeZoneSource)) {
$timeZoneSource = date_default_timezone_get();
}
if (empty($timeZoneTarget)) {
$timeZoneTarget = date_default_timezone_get();
}
$dt = new DateTime($dateString, new DateTimeZone($timeZoneSource));
$dt->setTimezone(new DateTimeZone($timeZoneTarget));
return $dt->format("Y-m-d H:i:s");
}
因此,要转换为服务器默认值,只需传递一个时区:
changeTimeZone("2016-10-24 16:28","Asia/Tokyo");
要从服务器默认值转换为用户,请将第二个参数留空:
changeTimeZone("2016-10-24 16:28","","Asia/Tokyo");
要在与默认值无关的时区之间切换,您将提供两个时区:
changeTimeZone("2016-10-24 16:28","America/New_York","Asia/Tokyo");
看起来效果不错!
很好的回答。我更喜欢尝试catch而不是is(空),以避免时区字符串中出现任何拼写错误。尝试new date timezone($timezonesource);catch(exception$e)$timezonesource=date_default_timezone_get();
datetime::setTimeZone--日期时区设置-设置datetime对象的时区
面向对象样式
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') ."
";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') ."
";
?>
程序风格
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') ."
";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') ."
";
?>
以上示例将输出:
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45
本地的UTC:
$datetime = date("Y-m-d H:i:s");
$utc = new DateTime($datetime, new DateTimeZone('UTC'));
$utc->setTimezone(new DateTimeZone('America/Sao_Paulo'));
echo $utc->format('Y-m-d H:i:s');
?>
//将日期从一个区域转换为另一个区域../*$zone_from='asia/kolkata';
$zone_to='America/Phoenix';
date_default_timezone_set($zone_from);
$convert_date="2016-02-26 10:35:00";
echo $finalDate=zone_conversion_date($convert_date, $zone_from, $zone_to);
*/
function zone_conversion_date($time, $cur_zone, $req_zone)
{
date_default_timezone_set("GMT");
$gmt = date("Y-m-d H:i:s");
date_default_timezone_set($cur_zone);
$local = date("Y-m-d H:i:s");
date_default_timezone_set($req_zone);
$required = date("Y-m-d H:i:s");
/* return $required; */
$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));
$date = new DateTime($time);
$date->modify("+$diff1 seconds");
$date->modify("+$diff2 seconds");
return $timestamp = $date->format("Y-m-d H:i:s");
}
$time='6:02';
$dt = new DateTime($time, new DateTimeZone('America/New_York'));
//echo $dt->format('Y-m-d H:i:s') . PHP_EOL;
$dt->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $dt->format('H:i') . PHP_EOL;
?>