I am no expert in php. I know timezones are supported in PHP.
For a given timezone TZ supported by PHP, I need to retrieve the offset (i.e., number of hours and minutes to add or substract) to a known UTC (i.e. GMT+0) to get that time in the TZ zone.
How can I achieve this? Ultimately, I need to get those offsets for all supported timezones in PHP. Thanks.
Last Updated : 16 Aug, 2018 The timezone_offset_get () function is an inbuilt function in PHP which is used to return the timezone offset from GMT. The date time object and the date-time are sent as a parameter to the timezone_offset_get () function and return the timezone offset in seconds on success or False on failure.
Unfortunately, PHP requires that you call date_default_timezone_set(). If you set that to GMT, your dates from a database will still be in local time, but date('Z') will return zero. If you set it to the server's timezone, you might as well just hard-code the server's offset from GMT in an include file.
The date time object and the date-time are sent as a parameter to the timezone_offset_get () function and return the timezone offset in seconds on success or False on failure. Parameters: This function accepts two parameter as mentioned above and described below:
What's the easiest way to get the UTC offset in PHP, relative to the current (system) timezone? returns the UTC offset in seconds. Thanks. Unfortunately, PHP requires that you call date_default_timezone_set (). If you set that to GMT, your dates from a database will still be in local time, but date ('Z') will return zero.
This will be useful for converting offset values into GMT format without any calculation
<?php
//target time zone
$target_time_zone = new DateTimeZone('America/Los_Angeles');
//find kolkata time
$date_time = new DateTime('now', $target_time_zone);
//get the exact GMT format
echo 'GMT '.$date_time->format('P');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With