How to handle timezones?
Permalink
I'm working on an addon where I have an expiration date set by the administrator and using that date I determine whether or not something should be displayed to users. The issue I'm running into is the differences caused by timezones.
When I'm having the user set the expiration date I'm using the datetime() function of the date_time helper. When that gets posted to my controller I save it like so:
Then, when I pull the expiration date/time and try to compare it against the current date/time I do it like so:
However, the $expires and $now dates apparently are in UTC and the comparison is never quite right. I'm assuming this is because of the timezone issues.
So my question is, how can i incorporate timezones into all of this so that when the administrators sets the expiration date it will work properly for any user, regardless of timezone.
Thanks in advance!
Blake
When I'm having the user set the expiration date I'm using the datetime() function of the date_time helper. When that gets posted to my controller I save it like so:
$expiresDate = $this->post('expires_dt'); $expiresHour = $this->post('expires_h'); $expiresMin = $this->post('expires_m'); $expiresAMPM = $this->post('expires_a'); $expires = $expiresDate . ' ' . $expiresHour . ':' . $expiresMin . ' ' . $expiresAMPM; $expiresDt = DateTime::createFromFormat('n/j/Y h:i a', $expires); //this is the format i use when i insert it into the db $expiresDt->format('Y-m-d H:i:s')
Then, when I pull the expiration date/time and try to compare it against the current date/time I do it like so:
//$n['expires'] is the expiration date pulled from the db $expires = new DateTime($n['expires']); $now = new DateTime(); if($expires <= $now){ return false; }
However, the $expires and $now dates apparently are in UTC and the comparison is never quite right. I'm assuming this is because of the timezone issues.
So my question is, how can i incorporate timezones into all of this so that when the administrators sets the expiration date it will work properly for any user, regardless of timezone.
Thanks in advance!
Blake
Hm, how would that work? This addon I'm working on is for creating alerts, so an example scenario would be:
An admin creates an alert that should take effect immediately and expires on 10/20/2013 at 7:00 PM Central Time.
If a user on the East coast (Eastern Time) visits the site they should be able to see it until 10/20/2013 at 8:00 PM (EST).
I'm pretty sure I have to factor timezones in don't I? Or is there a way I can make it work with timestamps?
Sorry if this is a stupid question, but I really haven't had to work with timezones like this very much and for some reason it is really tripping me up.
An admin creates an alert that should take effect immediately and expires on 10/20/2013 at 7:00 PM Central Time.
If a user on the East coast (Eastern Time) visits the site they should be able to see it until 10/20/2013 at 8:00 PM (EST).
I'm pretty sure I have to factor timezones in don't I? Or is there a way I can make it work with timestamps?
Sorry if this is a stupid question, but I really haven't had to work with timezones like this very much and for some reason it is really tripping me up.
Maybe you should look at the code behind the DateHelper.
The ENABLE_USER_TIMEZONES define should be enabled to handle timezones.
Having that enabled then users have a timezone associated, and the DateHelper methods may help...
The ENABLE_USER_TIMEZONES define should be enabled to handle timezones.
Having that enabled then users have a timezone associated, and the DateHelper methods may help...
timestamps are the number of seconds from 1970-01-01 00:00 utc, and a timestamp is not timezone dependant