Updated 27 April 2020
Timezone is a laravel package created by James Mills. The main task of timezone package is to set a timezone for different users for different time zones in the application, and show them their local time.
It works by listening for the user login event and setting the timezone in the database. It uses Laravel GeoIP to look up the user using an IP address.
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).
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).
You can simply show dates to your user by using the command below:
1 |
{{ Timezone::convertToLocal($post->created_at) }} |
Or directly use this blade directive:
1 |
@displayDate($post->created_at) |
Step 1: Install the package by the composer
1 |
composer require jamesmills/laravel-timezone |
Step 2: Publish database Migrations
1 |
php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=migrations |
Step 3: Run the database migrations. This will add a timezone column to your users table.
1 |
php artisan migrate |
Default will use the format jS F Y g:i:a and will not show the timezone
1 2 3 |
{{ Timezone::convertToLocal($post->created_at) }} // 4th July 2018 3:32:am |
If you wish you can set a custom format and also include a nice version of the timezone
1 2 3 |
{{ Timezone::convertToLocal($post->created_at, 'Y-m-d g:i', true) }} // 2018-07-04 3:32 New York, America |
This is completely optional, but in case you want to use custom configurations:
1 |
php artisan vendor:publish --provider="JamesMills\LaravelTimezone\LaravelTimezoneServiceProvider" --tag=config |
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.
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.
If you wish to customise the underlying torann/geoip package you can publish the config file by using the command below.
1 |
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config |
So, this is pretty how you can use timezone package in your own laravel application following the steps above. It’s one of the most ewasier to use package. Along with that, this package is very efficient.
Laravel news: Laravel Timezone
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.