简体   繁体   中英

PHP: DateTime::createFromFormat() with timezone

I want to convert date form from d/m/Y to Ymd with timezone offset. I am able to convert from d/m/Y to Ymd with this code:

$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

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() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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