如何在不更改默认时区的情况下在php中转换日期字符串的时区.我想将它本地转换为仅显示.不应修改php时区设置.
编辑: 我的源时间是UTC字符串,我想将其转换为不同的格式,保留时区为UTC,但PHP正在将其转换为本地时区.我使用的代码是:
date('Y-m-d H:i::s',strtotime($time_str));
Run Code Online (Sandbox Code Playgroud)
我如何保留时区?
$src_tz = new DateTimeZone('America/Chicago');
$dest_tz = new DateTimeZone('America/New_York');
$dt = new DateTime("2000-01-01 12:00:00", $src_tz);
$dt->setTimeZone($dest_tz);
echo $dt->format('Y-m-d H:i:s');
Run Code Online (Sandbox Code Playgroud)
请注意,如果源时间是UTC,您可以将一行更改为:
$dt = new DateTime("2000-01-01 12:00:00 UTC");
Run Code Online (Sandbox Code Playgroud)
编辑:看起来你想要去到 UTC.在这种情况下,只需使用构造函数"UTC"的参数$dest_tz,并使用原始代码块.(当然,$src_tz如果参数与默认时区相同,则可以省略该参数.)