Converting GMT time to local time using timezone offset in php - php

I need to display user's activities date as per the current time zone.
My approach -
Getting a timezone offset from javascript and storing it to the user's profile table.
When user logged in, getting time zone offset.
current date is working fine with time zone offset-
$offsetDiff = $_SESSION['TimeZone']*60;
$UserDateTime = time() + $offsetDiff;
$currentDate = date('Y-m-d',$UserDateTime);
Dateo other then today is not working properly -
$offsetDiff = $_SESSION['TimeZone']*60;
$UserDateTime = '2014-02-10 08:58:00'; + $offsetDiff;
$monthUser = date('Y-m-d',$UserDateTime);
Can anybody please let me know how can i show correct date according to time zone offset?

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.

Related

Convert timezone offset to hours & minutes preserving sign

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.

Converting GMT time to local time using timezone offset, not timezone identifier

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.

PHP file modified time use GMT offset setting to report correct time

I'm currently reporting file modified time like so:
$this->newScanData[$key]["modified"] = filemtime($path."/".$file);
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"]);
To me I thought there was nothing wrong with that but a user of my code is reporting the time being 4 hours out. The only reason why I can think of this is because the server is in a different timezone to the user. Each user has a variable I can use $gmt_offset that stores the time zone that user is in. $gmt_offset is stored as a basic float offset.
The server could be in any timezone, not necessarily in GMT-0. The server might not be in the same timezone as the user.
How do I get $modifiedtime to have the correct time for the user in his timezone based on $gmt_offset?
filemtime() will return a unix timestamp based on the server's clock. Since you have user to gmt offset available, you must convert the unix timestamp to GMT and then into user's timszone as follows:
<?php
list($temp_hh, $temp_mm) = explode(':', date('P'));
$gmt_offset_server = $temp_hh + $temp_mm / 60;
$gmt_offset_user = -7.0;
$timestamp = filemtime(__FILE__);
echo sprintf('
Time based on server time.........: %s
Time converted to GMT.............: %s
Time converted to user timezone...: %s
Auto calculated server timezone...: %s
',
date('Y-m-d h:i:s A', $timestamp),
date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600),
date('Y-m-d h:i:s A', $timestamp - $gmt_offset_server * 3600 + $gmt_offset_user * 3600),
$gmt_offset_server
);
// Output based on server timezone = PKT (+05:00 GMT) and user timezone = PDT (-07:00 GMT)
// Time based on server time.........: 2011-06-09 03:54:38 PM
// Time converted to GMT.............: 2011-06-09 10:54:38 AM
// Time converted to user timezone...: 2011-06-09 03:54:38 AM
// Auto calculated server timezone...: 5
What you need is the strtotime() function. Changed date to gmdate, converting your servers time to GMT
For example if you need the time format like 10:00:00
gmdate("H:i:s", strtotime($gmt_offset . " hours"));
More info here:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.gmdate.php
$modifiedtime = date($date_format." ".$time_format, $this->newScanData[$key]["modified"] + ($gmt_offset * 3600));
$gmt_offset should be of type float, not int -- some time zones can have fractional difference, like GMT +09:30 for Adelaide

Changing current user timezone based on server UTC offset and user UTC offset

im writing a twitter web service in php. When a user signs in, i receive this node:
<utc_offset>-18000</utc_offset>
I have to change the script's timezone so that it adapts to the user's real timezone. The only php function i have found for this is: date_default_timezone_set($timezone_identifier) but it won't let me use -18000 as a the $timezone_identifier parameter.
So, how can i change the current user timezone based on two values: Server UTC offset and User UTC offset
BTW, this is how i'm getting the server UTC offset value:
$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);
Any ideas? Thanks!
To get current server time
date_default_timezone_set(date_default_timezone_get());
echo date('Y-m-d H:i:s', time());
Output for Europe/Paris (my server settings; UTC+2)
2011-04-12 20:39:43
To get user's time by offset
$user_offset = '-18000';
date_default_timezone_set('UTC');
$diff = "$user_offset seconds";
if ((substr($diff,0,1) != '+') && (substr($diff,0,1) != '-')) $diff = '+' . $diff;
$usertime = strtotime($diff, time());
echo date('Y-m-d H:i:s', $usertime);
Output UTC-5 (Ecuador -> Quito time NO DST), php timezone identifier 'America/Guayaquil'.
2011-04-12 13:39:43
PHP.net manual:
Timezone offset in seconds. The offset
for timezones west of UTC is always
negative, and for those east of UTC is
always positive. (-43200 through
50400)
The date_default_timezone... functions expect a string giving something like "Africa/Luanda" or whatever.
I suggest programmatically searching through the timezone database for a matching offset. If I recall correctly, those are in minutes from UTC, so you should divide the offset you are given by 60.

PHP: Why is this code erroneously returning zero?

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;
}

Categories