Last Updated: June 11, 2021
·
1.015K
· psychoticmeow

Timezones in PHP are quite easy

// It's best to specify what format your dates are in:
const DATE_FORMAT = 'Y-m-d H:i:s';

// Always keep PHP in UTC time.
// Better yet to specify it in php.ini.
date_default_timezone_set('UTC');

// But know what timezone data is being entered in:
$date = DateTime::createFromFormat(
    DATE_FORMAT,
    '1986-08-18 02:19:00',
    new DateTimeZone('Australia/Adelaide')
);

// And know what timezone data is being displayed in:
$date->setTimezone(
    new DateTimeZone('America/Detroit')
);

// And everything will work out fine...
echo $date->format(DATE_FORMAT);
// >> 1986-08-17 12:49:00