Skip to content

Commit c140342

Browse files
committed
Updated singleton accessor. Updated README examples
1 parent 8537969 commit c140342

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

README.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,75 @@
11
# Laravel Timezones
22

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+
37
## Getting started
48

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.
610

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:
812

913
### 1. Using middleware
1014

1115
Useful when the app's timezone should be set by ther user's settings.
1216

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+
1334
### 2. Using a Service Provider
1435

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+
```
1652

1753
## Usage
1854

1955
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:
2056

2157
```php
58+
use Whitecube\LaravelTimezones\Casts\TimezonedDatetime;
59+
use Whitecube\LaravelTimezones\Casts\ImmutableTimezonedDatetime;
60+
2261
/**
2362
* The attributes that should be cast.
2463
*
2564
* @var array
2665
*/
2766
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',
3069
];
3170
```
3271

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:
3473

3574
```php
3675
use Carbon\Carbon;

src/Facades/Timezone.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Timezone extends Facade
88
{
99
public static function getFacadeAccessor()
1010
{
11-
return 'timezone';
11+
return \Whitecube\LaravelTimezones\Timezone::class;
1212
}
1313
}

src/ServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ServiceProvider extends BaseServiceProvider
1414
*/
1515
public function register()
1616
{
17-
$this->app->singleton('timezone', function ($app) {
17+
$this->app->singleton(Timezone::class, function ($app) {
1818
return new Timezone(config('app.timezone'));
1919
});
2020
}

0 commit comments

Comments
 (0)