├── .gitattributes ├── .gitignore ├── .php_cs ├── .scrutinizer.yml ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── composer.json └── src ├── Facades └── Timezone.php ├── LaravelTimezoneServiceProvider.php ├── Listeners └── Auth │ └── UpdateUsersTimezone.php ├── Timezone.php ├── config └── timezone.php └── database └── migrations └── add_timezone_column_to_users_table.php.stub /.gitattributes: -------------------------------------------------------------------------------- 1 | *.php linguist-language=PHP 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | vendor 4 | composer.lock 5 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | in(__DIR__ . '/src') 5 | ; 6 | 7 | return PhpCsFixer\Config::create() 8 | ->setUsingCache(false) 9 | ->setRules([ 10 | '@PSR2' => true, 11 | 'concat_space' => ['spacing' => 'one'], 12 | 'cast_spaces' => ['space' => 'single'], 13 | 'no_unused_imports' => true, 14 | 'blank_line_before_statement' => true, 15 | 'trailing_comma_in_multiline_array' => true, 16 | 'single_quote' => true 17 | ]) 18 | ->setFinder($finder) 19 | ; 20 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [tests/*] 3 | 4 | checks: 5 | php: 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | 20 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr2 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## [1.13.0] - 2023-02-17 8 | 9 | ### Removed 10 | - Removed requirement for `treeware/plant` package. 11 | 12 | ## [1.12.0] - 2023-02-15 13 | 14 | ### Added 15 | - Support for Laravel 10 (Thank you [laravelshift](https://github.com/laravel-shift) ([PR#87](https://github.com/jamesmills/laravel-timezone/pull/87)) 16 | 17 | ## [1.11.0] - 2022-02-16 18 | ### Added 19 | - Added Laravel 9 support. (Thank you [grantholle](https://github.com/grantholle)) ([PR#80](https://github.com/jamesmills/laravel-timezone/pull/80)) 20 | - Make the message shown to users when timezone is set configurable. (Thank you [samtlewis](https://github.com/samtlewis)) ([PR#66](https://github.com/jamesmills/laravel-timezone/pull/66)) 21 | - Added option to enable Carbon's translatedFormat. (Thank you [aldesrahim](https://github.com/aldesrahim)) ([PR#69](https://github.com/jamesmills/laravel-timezone/pull/69)) 22 | 23 | ## [1.8.0] - 2020-01-22 24 | ### Added 25 | - Added the ability to turn off flash messages and use others packages. (Thank you [amandiobm](https://github.com/amandiobm)) ([PR#15](https://github.com/jamesmills/laravel-timezone/pull/15)) 26 | 27 | ## [1.7.0] - 2020-01-21 28 | ### Added 29 | - Added the ability to lookup within any attribute on the request helper, by any key. (Thank you [amandiobm](https://github.com/amandiobm)) ([PR#13](https://github.com/jamesmills/laravel-timezone/pull/13)) 30 | 31 | ## [1.6.0] - 2020-01-16 32 | ### Added 33 | - Ability to change the default format (Thank you [amandiobm](https://github.com/amandiobm)) ([PR#12](https://github.com/jamesmills/laravel-timezone/pull/12)) 34 | ### Fixed 35 | - Formatting bug where blade directive was removing all spaces (Thank you [amandiobm](https://github.com/amandiobm)) ([PR#11](https://github.com/jamesmills/laravel-timezone/pull/11)) 36 | 37 | ## [1.5.0] - 2020-01-15 38 | ### Added 39 | - Ability to publish migration dynamically (Thank you [amandiobm](https://github.com/amandiobm)) ([PR#7](https://github.com/jamesmills/laravel-timezone/pull/7)) 40 | 41 | ## [1.4.0] - 2020-01-09 42 | ### Added 43 | - Added config option for overwriting database value (Thank you [micahhenshaw](https://github.com/micahhenshaw)) ([PR#5](https://github.com/jamesmills/laravel-timezone/pull/5)) 44 | 45 | ## [1.3.0] - 2019-10-21 46 | ### Added 47 | - Support to `laravel/framework:^6` ([604edb3](https://github.com/jamesmills/laravel-timezone/commit/604edb350bbf12f042b40bc0f0c7effd4a46bfe7)) 48 | - This CHANGELOG file ([29a8002](https://github.com/jamesmills/laravel-timezone/commit/29a800262c159f31fed0b52d79c9f2a41d871b6d)) -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to run `composer install` and then `make fix`. 44 | 45 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 46 | 47 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 48 | 49 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 50 | 51 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 52 | 53 | **Happy coding**! 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 James Mills 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | THIS := $(realpath $(lastword $(MAKEFILE_LIST))) 2 | HERE := $(shell dirname $(THIS)) 3 | 4 | .PHONY: fix lint test 5 | 6 | fix: 7 | $(HERE)/vendor/bin/php-cs-fixer fix --config=$(HERE)/.php_cs 8 | 9 | lint: 10 | $(HERE)/vendor/bin/php-cs-fixer fix --config=$(HERE)/.php_cs --dry-run 11 | 12 | test: lint 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Timezone 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) 5 | [![Licence](https://img.shields.io/packagist/l/jamesmills/laravel-timezone.svg?style=flat-square)](https://packagist.org/packages/jamesmills/laravel-timezone) 6 | [![Quality Score](https://img.shields.io/scrutinizer/g/jamesmills/laravel-timezone.svg?style=flat-square)](https://scrutinizer-ci.com/g/jamesmills/laravel-timezone) 7 | [![StyleCI](https://github.styleci.io/repos/142882574/shield?branch=master)](https://github.styleci.io/repos/142882574) 8 | [![Buy us a tree](https://img.shields.io/badge/treeware-%F0%9F%8C%B3-lightgreen?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone) 9 | [![Treeware (Trees)](https://img.shields.io/treeware/trees/jamesmills/laravel-timezone?style=flat-square)](https://plant.treeware.earth/jamesmills/laravel-timezone) 10 | 11 | An easy way to set a timezone for a user in your application and then show date/times to them in their local timezone. 12 | 13 | ## How does it work 14 | 15 | This package listens for the `\Illuminate\Auth\Events\Login` event and will then automatically set a `timezone` on your `user` model (stored in the database). 16 | 17 | This package uses the [torann/geoip](http://lyften.com/projects/laravel-geoip/doc/) package which looks up the users location based on their IP address. The package also returns information like the users currency and users timezone. [You can configure this package separately if you require](#custom-configuration). 18 | 19 | ## How to use 20 | 21 | You can show dates to your user in their timezone by using 22 | 23 | ```php 24 | {{ Timezone::convertToLocal($post->created_at) }} 25 | ``` 26 | 27 | Or use our nice blade directive 28 | 29 | ```php 30 | @displayDate($post->created_at) 31 | ``` 32 | 33 | [More examples below](#examples) 34 | 35 | ## Installation 36 | 37 | Pull in the package using Composer 38 | 39 | ``` 40 | composer require jamesmills/laravel-timezone 41 | ``` 42 | 43 | Publish database migrations 44 | 45 | ``` 46 | php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=migrations 47 | ``` 48 | 49 | Run the database migrations. This will add a `timezone` column to your `users` table. 50 | 51 | ``` 52 | php artisan migrate 53 | ``` 54 | 55 | ## Examples 56 | 57 | ### Showing date/time to the user in their timezone 58 | 59 | Default will use the format `jS F Y g:i:a` and will not show the timezone 60 | 61 | ```php 62 | {{ Timezone::convertToLocal($post->created_at) }} 63 | 64 | // 4th July 2018 3:32:am 65 | ``` 66 | 67 | If you wish you can set a custom format and also include a nice version of the timezone 68 | 69 | ```php 70 | {{ Timezone::convertToLocal($post->created_at, 'Y-m-d g:i', true) }} 71 | 72 | // 2018-07-04 3:32 New York, America 73 | ``` 74 | 75 | ### Using blade directive 76 | 77 | Making your life easier one small step at a time 78 | 79 | ```php 80 | @displayDate($post->created_at) 81 | 82 | // 4th July 2018 3:32:am 83 | ``` 84 | 85 | And with custom formatting 86 | 87 | ```php 88 | @displayDate($post->created_at, 'Y-m-d g:i', true) 89 | 90 | // 2018-07-04 3:32 New York, America 91 | ``` 92 | 93 | ### Saving the users input to the database in UTC 94 | 95 | This will take a date/time, set it to the users timezone then return it as UTC in a Carbon instance. 96 | 97 | ```php 98 | $post = Post::create([ 99 | 'publish_at' => Timezone::convertFromLocal($request->get('publish_at')), 100 | 'description' => $request->input('description'), 101 | ]); 102 | ``` 103 | 104 | ## Custom Configuration 105 | 106 | Publishing the config file is optional. 107 | 108 | ```php 109 | php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=config 110 | ``` 111 | 112 | ### Flash Messages 113 | 114 | When the timezone has been set, we display a flash message, By default, is configured to use Laravel default flash messaging, here are some of the optional integrations. 115 | 116 | [laracasts/flash](https://github.com/laracasts/flash) - `'flash' => 'laracasts'` 117 | 118 | [mercuryseries/flashy](https://github.com/mercuryseries/flashy) - `'flash' => 'mercuryseries'` 119 | 120 | [spatie/laravel-flash](https://github.com/spatie/laravel-flash) - `'flash' => 'spatie'` 121 | 122 | [mckenziearts/laravel-notify](https://github.com/mckenziearts/laravel-notify) - `'flash' => 'mckenziearts'` 123 | 124 | [usernotnull/tall-toasts](https://github.com/usernotnull/tall-toasts) - `'flash' => 'tall-toasts'` 125 | 126 | To override this configuration, you just need to change the `flash` property inside the configuration file `config/timezone.php` for the desired package. You can disable flash messages by setting `'flash' => 'off'`. 127 | 128 | ### Overwrite existing timezones in the database 129 | 130 | By default, the timezone will be overwritten at each login with the current user timezone. This behavior can be restricted to only update the timezone if it is blank by setting the `'overwrite' => false,` config option. 131 | 132 | ### Default Format 133 | 134 | By default, the date format will be `jS F Y g:i:a`. To override this configuration, you just need to change the `format` property inside the configuration file `config/timezone.php` for the desired format. 135 | 136 | ### Lookup Array 137 | 138 | This lookup array configuration makes it possible to find the remote address of the user in any attribute inside the Laravel `request` helper, by any key. Having in mind when the key is found inside the attribute, that key will be used. By default, we use the `server` attribute with the key `REMOTE_ADDR`. To override this configuration, you just need to change the `lookup` property inside the configuration file `config/timezone.php` for the desired lookup. 139 | 140 | ### User Message 141 | 142 | You may configure the message shown to the user when the timezone is set by changing the `message` property inside the configuration file `config/timezone.php` 143 | 144 | ### Underlying GeoIp Package 145 | 146 | If you wish to customise the underlying `torann/geoip` package you can publish the config file by using the command below. 147 | 148 | ```php 149 | php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config 150 | ``` 151 | 152 | ## Changelog 153 | 154 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 155 | 156 | ## Contributing 157 | 158 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 159 | 160 | ## License 161 | 162 | This package is 100% free and open-source, under the MIT license. Use it however you want. 163 | 164 | This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/jamesmills/laravel-timezone) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. 165 | 166 | ## Issues 167 | 168 | If you receive a message like `This cache store does not support tagging` this is because the `torann/geoip` package requires a caching driver which supports tagging and you probably have your application set to use the `file` cache driver. You can [publish the config file](#custom-configuration) for the `torann/geoip` package and set `'cache_tags' => null,` to solve this. [Read more about this issue here](https://github.com/jamesmills/laravel-timezone/issues/4#issuecomment-494648925). 169 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jamesmills/laravel-timezone", 3 | "license": "MIT", 4 | "description": "Timezone storage and retrieval for Laravel", 5 | "keywords": [ 6 | "laravel", 7 | "timezone" 8 | ], 9 | "authors": [ 10 | { 11 | "name": "James Mills", 12 | "email": "james@jamesmills.co.uk" 13 | } 14 | ], 15 | "require": { 16 | "php": ">=7.4", 17 | "laravel/framework": "5.6.*|5.7.*|5.8.*|^6|^7|^8|^9|^10.0|^11.0|^12.0", 18 | "torann/geoip": "^3.0", 19 | "nesbot/carbon": "^1.0|^2.0|^3.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "JamesMills\\LaravelTimezone\\": "src/", 24 | "JamesMills\\LaravelTimezone\\Database\\Seeds\\": "database/seeds/" 25 | } 26 | }, 27 | "extra": { 28 | "laravel": { 29 | "providers": [ 30 | "JamesMills\\LaravelTimezone\\LaravelTimezoneServiceProvider" 31 | ] 32 | } 33 | }, 34 | "require-dev": { 35 | "friendsofphp/php-cs-fixer": "^2.12|^3.14" 36 | }, 37 | "scripts": { 38 | "pre-package-install": [ 39 | "@php artisan config:clear" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Facades/Timezone.php: -------------------------------------------------------------------------------- 1 | publishes([ 31 | __DIR__ . '/database/migrations/add_timezone_column_to_users_table.php.stub' => database_path('/migrations/' . date('Y_m_d_His') . '_add_timezone_column_to_users_table.php'), 32 | ], 'migrations'); 33 | } 34 | 35 | // Register the Timezone alias 36 | AliasLoader::getInstance()->alias('Timezone', \JamesMills\LaravelTimezone\Facades\Timezone::class); 37 | 38 | // Register an event listener 39 | $this->registerEventListener(); 40 | 41 | // Allow config publish 42 | $this->publishes([ 43 | __DIR__ . '/config/timezone.php' => config_path('timezone.php'), 44 | ], 'config'); 45 | 46 | // Register a blade directive to show user date/time in their timezone 47 | Blade::directive( 48 | 'displayDate', 49 | function ($expression) { 50 | $options = explode(',', $expression); 51 | 52 | if (count($options) == 1) { 53 | return ""; 54 | } elseif (count($options) == 2) { 55 | return ""; 56 | } elseif (count($options) == 3) { 57 | return ""; 58 | } elseif (count($options) == 4) { 59 | return ""; 60 | } else { 61 | return 'error'; 62 | } 63 | } 64 | ); 65 | } 66 | 67 | /** 68 | * Register the service provider. 69 | * 70 | * @return void 71 | */ 72 | public function register() 73 | { 74 | $this->app->bind('timezone', Timezone::class); 75 | 76 | $this->mergeConfigFrom( 77 | __DIR__ . '/config/timezone.php', 78 | 'timezone' 79 | ); 80 | } 81 | 82 | /** 83 | * 84 | */ 85 | private function registerEventListener(): void 86 | { 87 | $events = [ 88 | \Illuminate\Auth\Events\Login::class, 89 | \Laravel\Passport\Events\AccessTokenCreated::class, 90 | ]; 91 | 92 | Event::listen($events, UpdateUsersTimezone::class); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/Listeners/Auth/UpdateUsersTimezone.php: -------------------------------------------------------------------------------- 1 | userId); 31 | 32 | return; 33 | } 34 | 35 | /** 36 | * If the event is Login, we get the user from the web guard. 37 | */ 38 | if ($event instanceof Login) { 39 | $user = Auth::user(); 40 | } 41 | 42 | /** 43 | * If no user is found, we just return. Nothing to do here. 44 | */ 45 | if (is_null($user)) { 46 | return; 47 | } 48 | 49 | $ip = $this->getFromLookup(); 50 | $geoip_info = geoip()->getLocation($ip); 51 | 52 | if ($user->timezone != $geoip_info['timezone']) { 53 | if (config('timezone.overwrite') == true || $user->timezone == null) { 54 | $user->timezone = $geoip_info['timezone'] ?? $geoip_info->time_zone['name']; 55 | $user->save(); 56 | 57 | $this->notify($geoip_info); 58 | } 59 | } 60 | } 61 | 62 | /** 63 | * @param Location $geoip_info 64 | */ 65 | private function notify(Location $geoip_info) 66 | { 67 | if (request()->hasSession() && config('timezone.flash') == 'off') { 68 | return; 69 | } 70 | 71 | $message = sprintf(config('timezone.message', 'We have set your timezone to %s'), $geoip_info['timezone']); 72 | 73 | if (config('timezone.flash') == 'laravel') { 74 | request()->session()->flash('success', $message); 75 | 76 | return; 77 | } 78 | 79 | if (config('timezone.flash') == 'laracasts') { 80 | flash()->success($message); 81 | 82 | return; 83 | } 84 | 85 | if (config('timezone.flash') == 'mercuryseries') { 86 | flashy()->success($message); 87 | 88 | return; 89 | } 90 | 91 | if (config('timezone.flash') == 'spatie') { 92 | flash()->success($message); 93 | 94 | return; 95 | } 96 | 97 | if (config('timezone.flash') == 'mckenziearts') { 98 | notify()->success($message); 99 | 100 | return; 101 | } 102 | 103 | if (config('timezone.flash') == 'tall-toasts') { 104 | toast()->success($message)->pushOnNextPage(); 105 | 106 | return; 107 | } 108 | } 109 | 110 | /** 111 | * @return mixed 112 | */ 113 | private function getFromLookup() 114 | { 115 | $result = null; 116 | 117 | foreach (config('timezone.lookup') as $type => $keys) { 118 | if (empty($keys)) { 119 | continue; 120 | } 121 | 122 | $result = $this->lookup($type, $keys); 123 | 124 | if (is_null($result)) { 125 | continue; 126 | } 127 | } 128 | 129 | return $result; 130 | } 131 | 132 | /** 133 | * @param $type 134 | * @param $keys 135 | * @return string|null 136 | */ 137 | private function lookup($type, $keys) 138 | { 139 | $value = null; 140 | 141 | foreach ($keys as $key) { 142 | if (! request()->$type->has($key)) { 143 | continue; 144 | } 145 | $value = request()->$type->get($key); 146 | } 147 | 148 | return $value; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/Timezone.php: -------------------------------------------------------------------------------- 1 | user()->timezone) ?? config('app.timezone'); 22 | 23 | $enableTranslation = $enableTranslation !== null ? $enableTranslation : config('timezone.enableTranslation'); 24 | 25 | $date->setTimezone($timezone); 26 | 27 | if (is_null($format)) { 28 | return $enableTranslation ? $date->translatedFormat(config('timezone.format')) : $date->format(config('timezone.format')); 29 | } 30 | 31 | $formatted_date_time = $enableTranslation ? $date->translatedFormat($format) : $date->format($format); 32 | 33 | if ($format_timezone) { 34 | return $formatted_date_time . ' ' . $this->formatTimezone($date); 35 | } 36 | 37 | return $formatted_date_time; 38 | } 39 | 40 | /** 41 | * @param $date 42 | * @return Carbon\Carbon 43 | */ 44 | public function convertFromLocal($date) : Carbon 45 | { 46 | return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC'); 47 | } 48 | 49 | /** 50 | * @param Carbon\Carbon $date 51 | * @return string 52 | */ 53 | private function formatTimezone(Carbon $date) : string 54 | { 55 | $timezone = $date->format('e'); 56 | $parts = explode('/', $timezone); 57 | 58 | if (count($parts) > 1) { 59 | return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0]; 60 | } 61 | 62 | return str_replace('_', ' ', $parts[0]); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/config/timezone.php: -------------------------------------------------------------------------------- 1 | 'laravel', 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Overwrite Existing Timezone 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here you may configure if you would like to overwrite existing 24 | | timezones if they have been already set in the database. 25 | | options [true, false] 26 | | 27 | */ 28 | 29 | 'overwrite' => true, 30 | 31 | /* 32 | |-------------------------------------------------------------------------- 33 | | Overwrite Default Format 34 | |-------------------------------------------------------------------------- 35 | | 36 | | Here you may configure if you would like to overwrite the 37 | | default format. 38 | | 39 | */ 40 | 41 | 'format' => 'jS F Y g:i:a', 42 | 43 | /* 44 | |-------------------------------------------------------------------------- 45 | | Enable translated output 46 | |-------------------------------------------------------------------------- 47 | | 48 | | Here you may configure if you would like to use translated output. 49 | | 50 | */ 51 | 52 | 'enableTranslation' => false, 53 | 54 | /* 55 | |-------------------------------------------------------------------------- 56 | | Lookup Array 57 | |-------------------------------------------------------------------------- 58 | | 59 | | Here you may configure the lookup array whom it will be used to fetch the user remote address. 60 | | When a key is found inside the lookup array that key it will be used. 61 | | 62 | */ 63 | 64 | 'lookup' => [ 65 | 'server' => [ 66 | 'REMOTE_ADDR', 67 | ], 68 | 'headers' => [ 69 | 70 | ], 71 | ], 72 | 73 | /* 74 | |-------------------------------------------------------------------------- 75 | | User Message 76 | |-------------------------------------------------------------------------- 77 | | 78 | | Here you may configure the message shown to the user when the timezone is set. 79 | | Be sure to include the %s which will be replaced by the detected timezone. 80 | | e.g. We have set your timezone to America/New_York 81 | | 82 | */ 83 | 84 | 'message' => 'We have set your timezone to %s', 85 | ]; 86 | -------------------------------------------------------------------------------- /src/database/migrations/add_timezone_column_to_users_table.php.stub: -------------------------------------------------------------------------------- 1 | string('timezone')->after('remember_token')->nullable(); 18 | }); 19 | } 20 | } 21 | 22 | /** 23 | * Reverse the migrations. 24 | * 25 | * @return void 26 | */ 27 | public function down() 28 | { 29 | Schema::table('users', function (Blueprint $table) { 30 | $table->dropColumn('timezone'); 31 | }); 32 | } 33 | } 34 | --------------------------------------------------------------------------------