I’m trying to get the user’s timezone on their first request using Zend Framework.
Currently, once I get the timezone, I store it into a $_SESSION variable.
Here is my action controller:
public function init()
{
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('timezone', 'html')->initContext();
}
public function timezoneAction()
{
$_SESSION['tz'] = $this->_getParam('tzName');
}
I’m aware that there is actually no way to access to this session variable during the first request through Ajax since JavaScript is a client-side script and the detection occurs after the first request. There’s also no way to get the user’s timezone through HTTP header.
Then, how to get a user’s timezone on his first request? Is geo-ip the only solution available? What are the best practices? Thanks.
If I’m reading you right, the problem is that:
The most direct solution is to do it all client-side (unless I’m missing some requirement — your question is not entirely clear about your context). Have the server output stuff in UTC, wrapped in some HTML element with a special class (ie:
<span class="datetime">2012-01-2 01:02:03 UTC</span>, and then have some javascript replace the content of those elements with some more user-friendly, localized, representation of the local time.Of course, JS doesn’t really give you the timezone. It just gives you an offset (via getTimezoneOffset()), but that should be enough for display purposes, unless I’m forgetting something (which is somewhat likely, as I have a vague recollection of finding this frustrating at one point, but I think it was because I wanted an actual timezone instead of an offset, because of daylight-savings).