yoshi yoshi - 12 days ago 5
PHP Question

PHP: DateTime::createFromFormat() with timezone

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:

$date = DateTime::createFromFormat('d/m/Y', $date);
$date = $date->format('Y-m-d');


But I am not sure how to add the timezone offset.

Answer

Just use DateTime::setTimeZone():

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