Skip to content

Instantly share code, notes, and snippets.

@aleskiontherun
Created August 20, 2014 15:11
Show Gist options
  • Save aleskiontherun/0c5e0045ee6953b037c2 to your computer and use it in GitHub Desktop.
Save aleskiontherun/0c5e0045ee6953b037c2 to your computer and use it in GitHub Desktop.
User timezone detection
<?php
// CLIENTSIDE:
if (!Yii::app()->user->timezone): ?>
<script type="text/javascript">
$(function () {
var visitortime = new Date();
$.ajax({
type: "GET",
url: "/user/settimezone",
data: {
name: /\((.*)\)/.exec(visitortime.toString())[1],
offset: -visitortime.getTimezoneOffset() / 60
}
});
});
</script>
<?php endif; ?>
<?php
// SERVER SIDE:
/**
* Set user timezone.
* @param string $name
* @param integer $offset
* @throws \CHttpException
*/
public function actionSetTimezone($name, $offset)
{
if (!Yii::app()->request->getIsAjaxRequest())
{
throw new \CHttpException(400);
}
$offset = intval($offset) * 3600;
$time_zone = timezone_name_from_abbr($name, $offset, false) ? : 'UTC';
Yii::app()->setTimeZone($time_zone);
Yii::app()->user->timezone = $time_zone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment