├── LICENSE.md ├── README.md ├── Timezone.php ├── TimezoneAction.php └── composer.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 yii2mod 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii 2 Timezone 2 | ============== 3 | Timezone component for Yii 2 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/yii2mod/yii2-timezone/v/stable)](https://packagist.org/packages/yii2mod/yii2-timezone) [![Total Downloads](https://poser.pugx.org/yii2mod/yii2-timezone/downloads)](https://packagist.org/packages/yii2mod/yii2-timezone) [![License](https://poser.pugx.org/yii2mod/yii2-timezone/license)](https://packagist.org/packages/yii2mod/yii2-timezone) 6 | 7 | Installation 8 | ------------ 9 | 10 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 11 | 12 | Either run 13 | 14 | ``` 15 | php composer.phar require --prefer-dist yii2mod/yii2-timezone "*" 16 | ``` 17 | 18 | or add 19 | 20 | ```json 21 | "yii2mod/yii2-timezone": "*" 22 | ``` 23 | 24 | to the require section of your composer.json. 25 | 26 | Usage 27 | ------------ 28 | > Currently firefox and edge browsers are not supported. 29 | 30 | Once the extension is installed, simply add component to your main config file: 31 | 32 | ```php 33 | 'bootstrap' => [ 34 | 'timezone' 35 | ] 36 | ..... 37 | 'components' => [ 38 | 'timezone' => [ 39 | 'class' => 'yii2mod\timezone\Timezone', 40 | 'actionRoute' => '/site/timezone' //optional param - full path to page must be specified 41 | ], 42 | ] 43 | 44 | ``` 45 | 46 | Then add new action to any controller (SiteController by default) 47 | 48 | ```php 49 | public function actions() 50 | { 51 | return [ 52 | 'timezone' => [ 53 | 'class' => 'yii2mod\timezone\TimezoneAction', 54 | ], 55 | ]; 56 | } 57 | 58 | ``` 59 | After configuration you can use ``` Yii::$app->timezone->name``` to get current user's timezone. 60 | -------------------------------------------------------------------------------- /Timezone.php: -------------------------------------------------------------------------------- 1 | actionRoute = Url::toRoute($this->actionRoute); 32 | $this->name = Yii::$app->session->get('timezone'); 33 | if ($this->name == null) { 34 | $this->registerTimezoneScript($this->actionRoute); 35 | $this->name = date_default_timezone_get(); 36 | } 37 | Yii::$app->setTimeZone($this->name); 38 | } 39 | 40 | /** 41 | * Registering script for timezone detection on before action event 42 | * @param $actionRoute 43 | */ 44 | public function registerTimezoneScript($actionRoute) 45 | { 46 | Yii::$app->on(Controller::EVENT_BEFORE_ACTION, function ($event) use ($actionRoute) { 47 | $view = $event->sender->view; 48 | $js = <<registerJs($js); 65 | }); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /TimezoneAction.php: -------------------------------------------------------------------------------- 1 | getRequest()->post('timezone', false); 21 | $zoneList = DateTimeZone::listIdentifiers(); 22 | 23 | if (empty($timezone) || !in_array($timezone, $zoneList)) { 24 | $timezoneAbbr = Yii::$app->getRequest()->post('timezoneAbbr'); 25 | $timezoneOffset = Yii::$app->getRequest()->post('timezoneOffset'); 26 | $timezone = timezone_name_from_abbr($timezoneAbbr, $timezoneOffset * 3600); 27 | } 28 | 29 | if (!$timezone || !in_array($timezone, $zoneList)) { 30 | $timezone = date_default_timezone_get(); 31 | } 32 | 33 | Yii::$app->session->set('timezone', $timezone); 34 | Yii::$app->end(); 35 | } 36 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yii2mod/yii2-timezone", 3 | "description": "Timezone helper", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2", "component"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Dmitry Semenov", 10 | "email": "disemx@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "*" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "yii2mod\\timezone\\": "" 19 | } 20 | } 21 | } 22 | --------------------------------------------------------------------------------