Gor*_*don 111
您可以使用datetime对象或其函数别名:
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');
Run Code Online (Sandbox Code Playgroud)
编辑评论
但我无法使用此方法,因为我需要在不同时区显示日期,因为用户从不同位置登录
那不是问题.当用户登录时,您确定他的时区并将其设置为DateTime对象,如图所示.我在我的一个项目中使用了类似的方法,它就像一个魅力.
在数据库中我需要在任何单个时区获取日期,然后才能正确处理它
您将时间存储为一个时区中的时间戳或日期时间.当您查询DateTime字段时,您可以将DateTime对象中的时间转换为此时区,或者 - 如果您的数据库支持它 - 使用所选时区进行查询.
sog*_*ger 18
一个更简单的方法看起来像这样:
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"));
Run Code Online (Sandbox Code Playgroud)
如PHP手册中所述,strtotime()也接受时区,您只需将其附加到日期时间即可.
我建议您以UTC格式存储所有日期时间,因为这样您就不会遇到夏令时问题.
saa*_*ada 12
这对我有用,它也很干净!
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);
Run Code Online (Sandbox Code Playgroud)
Ske*_*ets 10
这些答案都不适用于我(我跳过了尝试过于庞大的代码).我也认为仅为一次转换更改默认时区很奇怪.
这是我的解决方案:
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");
}
Run Code Online (Sandbox Code Playgroud)
因此,要转换为服务器默认值,您只需传递一个时区:
changeTimeZone("2016-10-24 16:28", "Asia/Tokyo");
Run Code Online (Sandbox Code Playgroud)
要将服务器默认值转换为用户,您可以将第二个参数保留为null或空白:
changeTimeZone("2016-10-24 16:28", "", "Asia/Tokyo");
Run Code Online (Sandbox Code Playgroud)
要在与默认值无关的时区之间切换,您需要提供2个时区:
changeTimeZone("2016-10-24 16:28", "America/New_York", "Asia/Tokyo");
Run Code Online (Sandbox Code Playgroud)
UTC 到本地:
<?php
$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');
?>
Run Code Online (Sandbox Code Playgroud)
DateTime :: setTimezone - date_timezone_set - 设置DateTime对象的时区
面向对象的风格
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Run Code Online (Sandbox Code Playgroud)
程序风格
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>
Run Code Online (Sandbox Code Playgroud)
以上示例将输出:
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
84086 次 |
最近记录: |