Created
August 20, 2014 15:11
-
-
Save aleskiontherun/0c5e0045ee6953b037c2 to your computer and use it in GitHub Desktop.
User timezone detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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