with string GMT/UTC +/-0400 or GMT/UTC +/-1000 based on local timings
Your custom format is just missing O
to give you the timezone offsets from local time.
Difference to Greenwich time [GMT] in hours Example: +0200
date_default_timezone_set['America/La_Paz'];
echo date['Y-m-d H:i:s O'];
2018-01-12 12:10:11 -0400
However, for maximized portability/interoperability, I would recommend using the ISO8601 date format c
date_default_timezone_set['America/La_Paz'];
echo date['c'];
2018-01-12T12:10:11-04:00
date_default_timezone_set['Australia/Brisbane'];
echo date['c'];
2018-01-13T02:10:11+10:00
You can use also
gmdate
and the timezone offset string will always be +00:00
date_default_timezone_set['America/La_Paz'];
echo gmdate['c'];
2018-01-12T16:10:11+00:00
date_default_timezone_set['Australia/Brisbane'];
echo gmdate['c'];
2018-01-12T16:10:11+00:00
Timezone conversion to a specific timezone helps to synchronize different DateTime. It is very useful to balance the server time and the user’s local time. PHP DateTime class provides an easy way to convert a date time stamp to UTC. You can convert any timezone to UTC DateTime using PHP.
In the example code snippet, we will show you how to convert local date&time to UTC DateTime [YYYY-MM-DD HH:MM:SS format] in PHP.
Convert Current DateTime to UTC DateTime using PHP:
$dateTime = date["Y-m-d H:i:s"];
$newDateTime = new DateTime[$dateTime];
$newDateTime->setTimezone[new DateTimeZone["UTC"]];
$dateTimeUTC = $newDateTime->format["Y-m-d H:i:s"];
Convert Local DateTime to UTC DateTime using PHP:
$dateTime = '2021-04-28 18:37:54';
$tz_from = 'America/New_York';
$newDateTime = new DateTime[$dateTime, new DateTimeZone[$tz_from]];
$newDateTime->setTimezone[new DateTimeZone["UTC"]];
$dateTimeUTC = $newDateTime->format["Y-m-d H:i:s"];
Get Current Date/Time in UTC Using DateTime
Class
To get the current time in UTC with the DateTime
class, we can simply specify the UTC time zone with an instance of DateTimeZone
like so:
$utcDateTime = new DateTime['now', new DateTimeZone['UTC']]; echo $utcDateTime->format['Y-m-d H:i:s']; // output: '2020-12-31 22:28:21'
Convert Existing Date/Time Into UTC Using DateTime
Class
To convert the time zone to UTC of an existing DateTime
class instance, we can do the following:
$utcDateTime->setTimezone[new DateTimeZone['UTC']]; echo $utcDateTime->format['Y-m-d H:i:s']; // output: '2020-12-31 22:28:21'
If the existing date/time is
not already an instance of DateTime
class, then we can convert it in the following ways:
$utcDateTime = new DateTime[$existingDateTime, new DateTimeZone['UTC']]; echo $utcDateTime->format['Y-m-d H:i:s']; // output: '2020-12-31 22:28:21'
If the existing date/time has a custom format [which the DateTime
object cannot automatically convert or recognize], then you can specify the date/time format in the following way and convert it to the DateTime
object:
$existingDateTime = date['Y m d H-i-s']; $utcDateTime = DateTime::createFromFormat[ 'Y m d H-i-s', $existingDateTime, new DateTimeZone['America/New_York'] ]; $utcDateTime->setTimezone[new DateTimeZone['UTC']]; echo $utcDateTime->format['Y-m-d H:i:s']; // output: '2020-12-31 22:28:21'
Hope you found this post useful. It was published 31 Dec, 2020. Please show your love and support by sharing this post.