Download the PHP package gazugafan/laravel-timezone without Composer
On this page you can find all versions of the php package gazugafan/laravel-timezone. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gazugafan/laravel-timezone
More information about gazugafan/laravel-timezone
Files in gazugafan/laravel-timezone
Package laravel-timezone
Short Description Date/time timezone storage and retrieval for Laravel 5
License MIT
Homepage https://github.com/gazugafan/laravel-timezone
Informations about the package laravel-timezone
Laravel Timezone
Date/time timezone storage and retrieval for Laravel 5
A simple set of functions to assist in converting date/times between a global storage timezone, and a user's local timezone.
Requirements
- This has been unit tested on Laravel 5.4 with PHP 7.1, but I believe it should work with older versions as well. Let me know if you find that it does!
Installation
Install via Composer...
Add the service provider to the array in ...
And add an alias to the array in ...
Overview
If your application is timezone-aware, you'll want to be converting all of your date/times to a single global timezone before storing them in the database. Then, when you retrieve these date/times, you'll want to convert them to the currently-logged-in user's local timezone before displaying them. This Timezone class contains a simple set of methods for doing just that!
Setup
First, you need to decide what global timezone you'll be using to store your date/times in. Most people recommend using UTC for this storage timezone, which is fair because it's commonly-used and somewhat expected. It really doesn't matter, though--so long as you pick one timezone and never change it. Personally, I recommend using whatever timezone you (or your DBA) is in. This way, when manually querying or browsing the database, you'll be seeing dates in your own timezone instead of UTC, which will probably be much easier to work with.
Got your storage timezone figured out? Good! Fill it into the entry in . It's the only place you'll need to specify it...
Next, you'll need to figure out how your user's timezone will be specified. A pretty straightforward way to do this would be adding a field to your user's table. By default, these helper methods will assume that's what you've done, and will retrieve the currently-logged-in user's timezone like this...
If you need help coming up with a list of timezones for your users to select from, here's a set of options you could use in a dropdown...
A whole bunch of options
If you've come up with some other way to get the currently-logged-in user's timezone, you can override this behavior by specifying your own callback function. Just to be safe, you'll probably want to return the app's default timezone (that you configured above), if no user is logged in...
Usage
There are two methods which you can use to convert date/times to and from the storage timezone. These are and . Both of them accept a date/time as the first parameter, which can be essentially any representation of a date, including DateTime, string, timestamp, another Carbon instance, etc. They'll also both return a Carbon instance of the converted date.
Use on a user--supplied date/time before storing it in the database. After retrieving a date/time from the database, use to convert it to the user's local timezone before displaying it. If you're doing an SPA, consider skipping entirely, and instead doing the conversion on the client-side using something like Moment.js.
Credits
Inspired by camroncade/timezone