PHP:DateTime::createFromFormat() 带时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24272477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: DateTime::createFromFormat() with timezone
提问by yoshi
I want to convert date form from d/m/Y to Y-m-d with timezone offset. I am able to convert from d/m/Y to Y-m-d with this code:
我想将日期格式从 d/m/Y 转换为带有时区偏移的 Ymd。我可以使用以下代码从 d/m/Y 转换为 Ymd:
$date = DateTime::createFromFormat('d/m/Y', $date);
$date = $date->format('Y-m-d');
But I am not sure how to add the timezone offset.
但我不确定如何添加时区偏移量。
回答by denoise
(PHP 5 >= 5.3.0) you actually enter the third parameter
(PHP 5 >= 5.3.0) 你实际上输入了第三个参数
public static DateTime DateTime::createFromFormat(string $format , string $time[, DateTimeZone $timezone])
public static DateTime DateTime::createFromFormat(string $format , string $time[, DateTimeZone $timezone])
$date = DateTime::createFromFormat('d/m/Y', $date, new DateTimeZone('Europe/Berlin'));
回答by John Conde
Just use DateTime::setTimeZone()
:
$date = DateTime::createFromFormat('d/m/Y', $date);
$date->setTimeZone(new DateTimeZone('America/New_York'));
$date = $date->format('Y-m-d');