How can I simply change the Timezone Offset with Zend_Date? - php

I'm using Zend_Date to manage dates/times in a scheduling app.
The only way I'm able to determine the user's Timezone is via Javascript which only gives a Timezone Offset. Zend_Date only seems to take Timezones in the 'America/New_York' format.
Is there a way to get the users timezone in this format or set the Timezone Offset with Zend_Date?
Thanks!

Nicky,
You don't really need Zend_Date for this as the PHP intrinsic DateTime and DateTimeZone objects work well for this; however, I can point you in the right direction if you really need to use Zend_Date.
See the following examples
$date = new Zend_Date(1234567890, false, $locale);
$date->toString... (see: http://framework.zend.com/manual/en/zend.date.constants.html for more details)
Use the following Constants for ISO 8601:
(Z = Difference of time zone [+0100])
This should get you to where you need to be. Please post code samples if you get stuck.

You could use the timezone_name_from_abbr function:
$javascript_offset = '-1';
$zone = timezone_name_from_abbr('', $javascript_offset * 3600, 0);
You will also need to consider DST. Have a look at this example for more info.

You can use javascript to set a cookie to the users timezone offset.
Then in php you can have an array indexed by offset
$timezones = array (
'-1' => 'adfas/adsfafsd',
'-2' => 'adsfasdf/asdfasdf'
...
);
$date->setTimezone( $timezones[$_COOKIE['tz_offset']] );

Related

Change the ISO 8601 date format using PHP

I am trying to get the current date/time using Data Type: ISODateTime and Format: YYYY-MM-DDTHH:MM:SS e.g. 2012-02-06T08:35:30. I searched how I would do this in PHP and found that I can use;
$formatedDate = date("c");
Although the output of this is almost correct is not quite what I need and I can't figure out how to alter it, the current output of this is;
2014-07-01T10:53:10+02:00
My problem is I need to remove the "+02:00" and also this time is an hour ahead of my local time, which is what I need. Therefore, in this example, I would require;
2014-07-01T09:53:10
Any help would be really appreciated. Thanks.
You could just format the date manually.
$formattedDate = date('Y-m-d\TH:i:s');
To get your local time, use date_default_timezone_set() to set the appropriate timezone (before declaring $formattedDate).
// Change 'America/New_York' to your timezone
date_default_timezone_set('America/New_York');
See demo
To find your timezone, see the List of Supported Timezones.

Ignore BST with php strtotime() function

Is there a way for me to tell the strtotime() function not to change the time I give it into BST? i.e. if do
date('g.ia', strtotime("2014-06-25T19:30"))
I want to get 7:30pm, just as if I entered
date('g.ia', strtotime("2014-06-25T19:30"))
(The first one currently returns 6:30pm)
I'm aware I could just write a manual check for the day/month and add an hour if necessary, or just parse the time myself from the string, but both solutions sound a bit messy (I'll have to do this in quite a few places).
Sorry if there's something obvious I'm missing, pretty new to php
Function date() will format time based on your timezone setting. Said that, your example doesn't make sense since strtotime() will use current timezone setting to convert input to unix timestamp, and then function date() will use again that timezone setting to format timestamp back. You must be changing timezone setting between strtotime() and date() function calls, like this demo.
You can simply use DateTime extension, where you implicitly tell in what timezone is your time:
$dt = new DateTime('2014-06-25T19:30', new DateTimezone('Europe/London'));
echo $dt->format('g.ia');

Convert GMT time to Day light saving countries Timezone

I am working on project in which i am converting the time according to user location. i.e if user belongs to ALASKA then then all the information will be shown according to his countries timezone.I did some conversion but i am getting wrong time in case of user has a Daylight timezone (DST) which changes frequently.
Here is my code snippet.
date_default_timezone_set('UTC');
$offset=$_SESSION['gmtoffset'];
echo date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))-(-$offset));
So in this case how do i maintain this timezone? what is the exact way for this time related conversions?
Any guidance and idea will be appreciated.
Thanks
Here's a simple solution
Javascript:
var timeZone = new Date().getTimezoneOffset();
document.cookie="tz=" + (timeZone/(-60));
Then PHP:
if(!empty($_COOKIE['tz']) && $_COOKIE['tz']>=-12 && $_COOKIE['tz']<=13){
$offset = $_COOKIE['tz'];
$tz = timezone_name_from_abbr(null, $offset * 3600, true);
if($tz === false) $tz = timezone_name_from_abbr(null, $offset * 3600, false);
date_default_timezone_set($tz);
}else{
date_default_timezone_set('UTC');
}
I am a bit uncertain to what in particular you are asking, but hopefully this will help.
If your question is regarding detecting that the user is in the Alaska time zone, then read:
How to detect user's timezone?
If your question is about picking a time zone by country, then read:
In PHP, how can I get Timezone from offset and country?
If you are wanting to understand Alaskan time zones, recognize that there are three different legal zones, and seven different IANA/Olson/TZ time zones (that PHP can use):
http://en.wikipedia.org/wiki/Time_in_Alaska
Don't try to treat a time zone as a numeric value. See "TimeZone != Offset" in the timezone tag wiki.
If you are wanting to manipulate dates with time zones in PHP, see the example in the PHP documentation for date_default_timezone_set.
If you just want to work with UTC, then there's no reason to do anything with time zones at all. The browser can always convert UTC to and from the user's local time zone, whatever that might be. Just create your JavaScript Date objects with a UTC timestamp integer, or parse from a UTC format, such as YYYY-MM-DDTHH:MM:SSZ.
To eliminate browser inconsistencies, you might want to use a library instead, such as moment.js:
// here's one way
var m = moment('2013-05-31T12:34:56Z');
// here's another
var m = moment.utc('2013-05-31 12:34:56','YYYY-MM-DD HH:mm:ss');
// pretty much anything else you might want to do...

Is it possible to create a DateTimeZone from an offset?

The DateTimeZone constructor only accepts a region name:
new DateTimeZone('Europe/London');
And not an offset from UTC:
new DateTimeZone('+01:00'); // Unknown or bad timezone (+01:00)
However, it is possible to obtain such a DateTimeZone from a DateTime:
(new DateTime('2012-12-28T00:00:00+01:00'))->getTimezone()->getName(); // +01:00
So this is a bit weird. Is there a way to directly obtain a DateTimeZone from an offset?
In addition to RafaƂ's answer, the simplest way I've found so far is:
DateTime::createFromFormat('O', '+01:00')->getTimezone();
Edit
This was a bug that has been fixed in PHP 5.5.10. It now works!
Modern answer:
new DateTimeZone('+0100');
Documentation:
http://php.net/manual/en/datetimezone.construct.php
Check out this function.
http://pl1.php.net/manual/en/function.timezone-name-from-abbr.php
You'll need to convert hours to seconds and pass them as second parameter.
Sth. like new DateTimeZone(timezone_name_from_abbr('', 3600, 0)) should work.
I dont think that there is any predefined way you wanna go. But if you declare any global function that will return you date and time with the offset added, it might help you.
Example :
function getDateTime($format="dd-mm-YY"){
$currDate= date($format);
$currDate=date($format,strtotime("+1 day",$currDate); // or whatever needed instead of +1 day
}

Zend_Date adding the timezone to a subtraction result

I have this code:
<?php
$start = new Zend_Date("2011-09-06T10:00:00+02:00",Zend_Date::ISO_8601);
$end = new Zend_Date("2011-09-06T10:01:00+02:00",Zend_Date::ISO_8601);
echo $end->sub($start);
?>
In short: I create two dates, with a minute's difference between them. Then I print out the difference (subtraction) between them.
The result, however, is:
01-01-1970 02:01:00
Basically, what I understand from this behaviour is that Zend_Date operates on dates without taking timezone into consideration, and then puts the timezone back in the result. Of course, this means that the subtraction result is off by the value of the timezone (+2h in my case).
What's the best way to get around this?
Yes, when echo'ing a Zend_Date, it will take your timezone into account. To get the difference formatted for GMT dates, you have to set the timezone explicitly:
$start = new Zend_Date("2011-09-06T10:00:00+02:00",Zend_Date::ISO_8601);
$end = new Zend_Date("2011-09-06T10:01:00+02:00",Zend_Date::ISO_8601);
echo $end->sub($start)->setTimezone('GMT')->get(Zend_Date::ISO_8601);
This would output: 1970-01-01T00:01:00+00:00
On a sidenote, if you do not need the dynamic localization features of Zend_Date it's best to avoid it in favor or PHP's native DateTime API. There is really no reason to use Zend_Date just because it exists in ZF. PHP's own DateTime API is faster and easier to use. Getting the time difference with the DateTime API would be
$start = new DateTime("2011-09-06T10:00:00+02:00");
$end = new DateTime("2011-09-06T10:01:00+02:00");
echo $start->diff($end)->format('%H:%I:%S');
which would output 00:01:00

Categories