简体   繁体   English

PHP:DateTime::createFromFormat() 带时区

[英]PHP: DateTime::createFromFormat() with timezone

I want to convert date form from d/m/Y to Ymd with timezone offset.我想将日期格式从 d/m/Y 转换为带有时区偏移的 Ymd。 I am able to convert from d/m/Y to Ymd with this code:我可以使用以下代码从 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.但我不确定如何添加时区偏移量。

(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'));

Just use DateTime::setTimeZone() :只需使用DateTime::setTimeZone()

$date = DateTime::createFromFormat('d/m/Y', $date);
$date->setTimeZone(new DateTimeZone('America/New_York'));
$date = $date->format('Y-m-d');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM