|
1 | 1 | # Laravel Timezones
|
2 | 2 |
|
| 3 | +Dealing with timezones can be a frustrating experience. Here's an attempt to brighten your day. |
| 4 | + |
| 5 | +**The problem:** it is commonly agreed that dates should be stored as `UTC` datetimes in the database, which generally means they also need to be adapted for the local timezone before manipulation or display. Laravel provides a `app.timezone` configuration, making it possible to start working with timezones. However, changing that configuration will affect both the stored and manipulated date's timezones. This package tries to address this by providing a timezone conversion mechanism that should perform most of the repetitive timezone configurations out of the box. |
| 6 | + |
3 | 7 | ## Getting started
|
4 | 8 |
|
5 |
| -All database dates should be stored using the `app.timezone` config setting. We highly suggest keeping it as `UTC` since it's a global standard for dates storage. |
| 9 | +The `app.timezone` configuration setting has to be set to the timezone that should be used when saving dates in the database. We highly recommend keeping it as `UTC` since it's a global standard for dates storage. |
6 | 10 |
|
7 |
| -For in-app date manipulation and display, you can define the timezone all dates should cast to using one of the following methods. Depending on you app's context, choose the one that best suits your situation. |
| 11 | +For in-app date manipulation and display, one would expect more flexibility. That's why it is possible to set the application's timezone dynamically by updating the `timezone` singleton instance. Depending on the app's context, please choose one that suits your situation best: |
8 | 12 |
|
9 | 13 | ### 1. Using middleware
|
10 | 14 |
|
11 | 15 | Useful when the app's timezone should be set by ther user's settings.
|
12 | 16 |
|
| 17 | +```php |
| 18 | +namespace App\Http\Middleware; |
| 19 | + |
| 20 | +use Closure; |
| 21 | +use Whitecube\LaravelTimezones\Facades\Timezone; |
| 22 | + |
| 23 | +class DefineApplicationTimezone |
| 24 | +{ |
| 25 | + public function handle($request, Closure $next) |
| 26 | + { |
| 27 | + Timezone::set($request->user()->timezone ?? 'Europe/Brussels'); |
| 28 | + |
| 29 | + return $next($request); |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
13 | 34 | ### 2. Using a Service Provider
|
14 | 35 |
|
15 |
| -Useful when the app's timezone should be set by the application itself. |
| 36 | +Useful when the app's timezone should be set by the application itself. For instance, in `App\Providers\AppServiceProvider`: |
| 37 | + |
| 38 | +```php |
| 39 | +namespace App\Providers; |
| 40 | + |
| 41 | +use Illuminate\Support\ServiceProvider; |
| 42 | +use Whitecube\LaravelTimezones\Facades\Timezone; |
| 43 | + |
| 44 | +class AppServiceProvider extends ServiceProvider |
| 45 | +{ |
| 46 | + public function boot() |
| 47 | + { |
| 48 | + Timezone::set('America/Toronto'); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
16 | 52 |
|
17 | 53 | ## Usage
|
18 | 54 |
|
19 | 55 | Once everything's setup, the easiest way to manipulate dates configured with the app's current timezone is to use the `TimezonedDatetime` or `ImmutableTimezonedDatetime` cast types on your models:
|
20 | 56 |
|
21 | 57 | ```php
|
| 58 | +use Whitecube\LaravelTimezones\Casts\TimezonedDatetime; |
| 59 | +use Whitecube\LaravelTimezones\Casts\ImmutableTimezonedDatetime; |
| 60 | + |
22 | 61 | /**
|
23 | 62 | * The attributes that should be cast.
|
24 | 63 | *
|
25 | 64 | * @var array
|
26 | 65 | */
|
27 | 66 | protected $casts = [
|
28 |
| - 'published_at' => \Whitecube\LaravelTimezones\Casts\TimezonedDatetime::class, |
29 |
| - 'birthday' => \Whitecube\LaravelTimezones\Casts\ImmutableTimezonedDatetime::class . ':Y-m-d', |
| 67 | + 'published_at' => TimezonedDatetime::class, |
| 68 | + 'birthday' => ImmutableTimezonedDatetime::class . ':Y-m-d', |
30 | 69 | ];
|
31 | 70 | ```
|
32 | 71 |
|
33 |
| -In other scenarios, use the `Timezone` Facade directly for conversion: |
| 72 | +In other scenarios, feel free to use the `Timezone` Facade directly for more convenience: |
34 | 73 |
|
35 | 74 | ```php
|
36 | 75 | use Carbon\Carbon;
|
|
0 commit comments