Laravel Timezone

James Mills > Code & Development > Laravel Timezone

Laravel Timezone is a Laravel Package making it easy to set a timezone for a user in your application and then show date/times to them in their local timezone. Other packages seem to only provide helper functions to convert to storage and from storage. My packages also provides an helpful way of setting the timezone on the user automatically.

Working across different timezones in your Laravel application doesn’t have to be complicated. Follow these simples rules and everything will be easy. Make sure you set your application to use a base timezone. I would recommend you use UTC. When you save an item in your database don’t try and do anything clever. Just make sure it’s saved as UTC. Simple.

Storing everything using one global timezone is the key. When you want to display the date/time of an item from your database to the user on the frontend you would normally do something like this.

$post->created_at->setTimezone('Asia/Dubai')->format('jS F Y g:i:a');

What’s missing from this setup is knowing what your users timezone is. Most of the applications I build require the user to be logged in so I created a simple package which listens for a “log in” event and automatically gets the users timezone using a GeoIP package.

Once you have installed the Laravel Timezones package into your application you will get access to a handful of helpful helpers and blade directives. if you are using Blade as your frontend templating engion then you can swap the above example to this

@displayDate($post->created_at)

And this will automatically display the blog posts created date and time to the user in their timezone (e.g. 4th July 2018 3:32:am). If the user is not logged in or they don’t have a timezone set it will default to the applications timezone with you have set in your config (which should be UTC).

Saving a date/time

Displaying a date/time to the user is easy enough with the help of the Laravel Timezones package. What about when you want to enable the user to set a date in your system?

A great example of this is for an event. In your application, you will have a form which will have an input box for the user to set a date and time for when the event will start. The user will pick the date and time in their timezone, this is natural and expected. So when you save this to the database you need to transform this to UTC before you save it.

$event = Event::create([
    'start_at' => Timezone::convertFromLocal($request->get('start_at')),
    'description' => $request->input('description'),
]);

Bonus extras

If your users are travelling and they change timezones then this is more than often picked up and automatically updated. The package listens for the ‘\Illuminate\Auth\Events\Login::class’ event and then uses the ‘UpdateUsersTimezone::class’ to update the users timezone.

The package has a simple integration with the Laracasts Flash package which provides a really simple and elegant solution to showing your users flash messages from your session. If you publish the Laravel Timezones package config then your users will get a notification when their timezone is updated. Just remember to include the Laracasts Flash message code in your view files.

One thought on “Laravel Timezone

Leave a Reply

Your email address will not be published. Required fields are marked *