I am attempting to retrieve a date that is not in GMT and convert it to GMT. To do this, I am creating two time zones (one GMT and one non-GMT) and attempting to get the offset between them. However, the offset is only correct in one direction. For this specific example, I am trying to compare GMT +4 to GMT. I expect to get 4hrs (14400 seconds) when I compare the the GMT timezone to the GMT+4 timezone, and -4hrs (-14400 seconds) when I compare the GMT+4 timezone to GMT. However, when comparing the later I'm getting 0... Here is what I have
$default_timezone = new DateTimeZone(drupal_get_user_timezone());
$default_reg_date = new DateTime($reg_date_string, $default_timezone);
$gmt_timezone = new DateTimeZone('UTC');
$gmt_reg_date = new DateTime($reg_date_string, $gmt_timezone);
// Returns as 14400
$default_gmt_offset = $default_timezone->getOffset($gmt_reg_date);
// Returns as 0
$gmt_default_offset = $gmt_timezone->getOffset($default_reg_date);
Why can't I get the right number, what am I doing wrong? Does retrieving the offset only work one way?
Note: in this specific example, drupal_get_user_timezone() is returning GMT+4
From the PHP documentation :
This function returns the offset to GMT for the date/time specified in the datetime parameter. The GMT offset is calculated with the timezone information contained in the DateTimeZone object being used.
The function works with 2 logics steps :
Convert the date into the timezone on which the function is applied
Get the offset from GMT
So calling this function on new DateTimeZone('UTC') will always returns 0
If you want to convert a date into UTC, you can use the setTimeZone function :
$date_string = '2020-05-01 09:11:00' ;
$date = new DateTime($date_string, new DateTimeZone('Europe/Brussels'));
echo $date->format('c') ; // 2020-05-01T09:11:00+02:00
$date->setTimeZone(new DateTimeZone('UTC'));
echo $date->format('c') ; // 2020-05-01T07:11:00+00:00
Related
I know \DateTimeZone::listIdentifiers() returns all timezone identifiers, but how can I retrieve their actual offset from UTC?
There is method getoffset
<?php
// Create two timezone objects, one for Taipei (Taiwan) and one for
// Tokyo (Japan)
$dateTimeZoneTaipei = new DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new DateTimeZone("Asia/Tokyo");
// Create two DateTime objects that will contain the same Unix timestamp, but
// have different timezones attached to them.
$dateTimeTaipei = new DateTime("now", $dateTimeZoneTaipei);
$dateTimeJapan = new DateTime("now", $dateTimeZoneJapan);
// Calculate the GMT offset for the date/time contained in the $dateTimeTaipei
// object, but using the timezone rules as defined for Tokyo
// ($dateTimeZoneJapan).
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);
// Should show int(32400) (for dates after Sat Sep 8 01:00:00 1951 JST).
var_dump($timeOffset);
?>
You can build an associative array mapping timezones and their offset using the getOffset method of DateTime instance.
getOffset returns the difference in seconds of a date evaluated in the time zone of the provided datetime argument (in our example, this is the current time in utc), and the same datetime as evaluated in the local time zone.
$utcNow = new DateTime('now', (new DateTimeZone('UTC')));
$identOffset = array_map(
function ($ident) use ($utcNow) {
$localTimeZone = new DateTimeZone($ident);
$offset = $localTimeZone->getOffset($utcNow);
return [$ident => $offset] ;
},
DateTimeZone::listIdentifiers()
);
$identOffset = array_merge(...$identOffset);
var_dump($identOffset);
If you are using https://carbon.nesbot.com/
// You will get timezone offset as string
Carbon::now('Asia/Kathmandu')->getOffsetString(); // +05:45
I'm trying to understand how PHP uses the given datetime to return getOffset result on a DateTimeZone object. As far as I see, the result is always the same no matter what datetime I pass.
public function testGetOffset()
{
// UTC offset is 00:00 (0 seconds)
$timeZoneUtc = new \DateTimeZone('UTC');
$dateUTC = new \DateTime('now', $timeZoneUtc);
// America/Sao_Paulo offset is -02:00 (-7200 seconds)
$timeZoneSP = new \DateTimeZone('America/Sao_Paulo');
$dateSP = new \DateTime('now', $timeZoneSP);
// America/New_York offset is -05:00 (-18000 seconds)
$timeZoneNY = new \DateTimeZone('America/New_York');
$dateNY = new \DateTime('now', $timeZoneNY);
$this->assertEquals(0, $timeZoneUtc->getOffset($dateUTC)); // true
$this->assertEquals(0, $timeZoneUtc->getOffset($dateNY)); // true
$this->assertEquals(0, $timeZoneUtc->getOffset($dateSP)); // true
$this->assertEquals(-7200, $timeZoneSP->getOffset($dateUTC)); // true
$this->assertEquals(-7200, $timeZoneSP->getOffset($dateNY)); // true
$this->assertEquals(-7200, $timeZoneSP->getOffset($dateSP)); // true
$this->assertEquals(-18000, $timeZoneNY->getOffset($dateUTC)); // true
$this->assertEquals(-18000, $timeZoneNY->getOffset($dateNY)); // true
$this->assertEquals(-18000, $timeZoneNY->getOffset($dateSP)); // true
}
Can anyone help me get that?
The offset changes only during a DST transition. New York will transition from winter time to summer time on March 11. getOffset tells you what the offset for that timezone is at that particular time. All the times you're giving it are all today, well before the DST transition date, so they all result in the same offset. Try getting the offset for a date before and after March 11 to see a difference.
A date is an absolute moment in time, it doesn't depend on a timezone. The timezone is used only when the date is formatted as string.
Internally, all three DateTime objects created by the code store the same value: the number of seconds (and microseconds on PHP 7) since 1970-01-01 00:00:00 UTC.
Suppose I have an offset like this:
$secOffset = -28800
I need it to convert in a format that is usable with MYSQL function convert_tz()
If I use gmdate("h:i", secOffset) I got 16:00 that is almost correct but It miss the minus - (that is quite important)
Alternative approach:
Suppose I have a time string America/Los_Angeles and I want to convert in a offset useful for MYSQL function convert_tz()
The final expected output is the offset from GMT so given a timezone.
For ex, having a timezone like America/Los_Angeles, the output should be:
−08:00
I'm on Magento/Zend so if any available function is on Zend I can accept answer based on it.
You should use the DateTimeZone and DateInterval classes for this:
$tzid = 'America/Los_Angeles';
$tz = new DateTimeZone($tzid);
$date = new DateTime('now', $tz);
// create a new date offset by the timezone offset
// gets the interval as hours & minutes
$offset = $tz->getOffset($date) . ' seconds';
$dateOffset = clone $date;
$dateOffset->sub(DateInterval::createFromDateString($offset));
$interval = $dateOffset->diff($date);
$formatted = $interval->format('%R%H:%I');
This is a little convoluted, as you first get the timezone offset in seconds, and then use DateTime to help convert that interval into hours/mins.
It's pretty easy to convert a given GMT date into local time if you're given the timezone identifier from this list in PHP: http://www.php.net/manual/en/timezones.php
For example, you can do this (where $fromTimeZone is just 'GMT', $toTimeZone is just one of the constants from that list (i.e. 'America/Chicago'), and $datetime is the GMT date):
public static function convertToTimezone($datetime, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i')
{
// Construct a new DateTime object from the given time, set in the original timezone
$convertedDateTime = new DateTime($datetime, timezone_open($fromTimeZone));
// Convert the published date to the new timezone
$convertedDateTime->setTimezone(timezone_open($toTimeZone));
// Return the udpated date in the format given
return $convertedDateTime->format($format);
}
However, I'm having issue converting the same GMT date to the local time if just given the timezone offset. For instance, instead of being given 'America/Chicago', I'm given -0500 (which is the equivalent offset for that timezone).
I've tried things such as the following (where $datetime is my GMT date and $toTimeZone is the offset (-0500 in this case)):
date($format, strtotime($datetime . ' ' . $toTimeZone))
I know all the date() sort of functions are based on the servers's timezone. I just can't seem to get it to ignore that and use a timezone offset that is given explicitly.
You can convert a specific offset to a DateTimeZone:
$offset = '-0500';
$isDST = 1; // Daylight Saving 1 - on, 0 - off
$timezoneName = timezone_name_from_abbr('', intval($offset, 10) * 36, $isDST);
$timezone = new DateTimeZone($timezoneName);
Then you can use it in a DateTime constructor, e.g.
$datetime = new DateTime('2012-04-21 01:13:30', $timezone);
or with the setter:
$datetime->setTimezone($timezone);
In the latter case, if $datetime was constructed with a different timezone, the date/time will be converted to specified timezone.
I'm calculating the offset between two timezones, but I'm seeing a result I don't expect (zero) with the following code:
$datetimezone_london = new DateTimeZone('Europe/London');
$datetimezone_client = new DateTimeZone('Australia/Canberra');
$now_client = new DateTime("now", $datetimezone_client);
$offset = $datetimezone_london->getOffset($now_client);
echo $offset;
If I flip the timezone strings, it works, but surely the above code should work too. What's happening?
getOffset() returns the offset to GMT in seconds, and London is currently on GMT, hence the return value is zero.
I believe what you need instead is:
$tz_london = new DateTimeZone('Europe/London');
$tz_client = new DateTimeZone('Australia/Canberra');
$time_london = new DateTime('now', $tz_london);
$time_client = new DateTime('now', $tz_client);
$offset = $time_client->getOffset() - $time_london->getOffset();
echo $offset;
This currently (in January) returns 39600 (11 hours). In July it returns 9 hours, and in mid October (where there's a short period when both Europe and Australia are in daylight saving) it returns 10 hours.
DateTimeZone::getOffset() works a bit differently that what most people think. It calculates the offset to GMT of the instance DateTimeZone offset for the date passed as parameter. The date passed as parameter is then converted to the same timezone as the instance (DST and other rules applying) and the offset is calculated for that date.
So your code right now calculates the offset to GMT of the timezone Europe/London.. Since Europe/London is on GMT right now (versus BMT), you are getting 0. (Try a date in August, you'll get 36000).
If you want the current difference between two timezones, use this code...
function timezone_diff($origin, $compareTo, $forDate = "now") {
$dtzOrigin = new DateTimeZone($origin);
$dtzCompareTo = new DateTimeZone($compareTo);
$compareDate = new DateTime($forDate);
$offsetOrigin = $dtzOrigin->getOffset($compareDate);
$offsetCompareTo = $dtzCompareTo->getOffset($compareDate);
return $offsetCompareTo - $offsetOrigin;
}