Skip to content

Commit f84f002

Browse files
committed
Added intro example
1 parent e00262f commit f84f002

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ Dealing with timezones can be a frustrating experience. Here's an attempt to bri
44

55
**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.
66

7+
```php
8+
// Model:
9+
protected $casts = [
10+
'occurred_at' => TimezonedDatetime::class,
11+
];
12+
13+
// Set a custom timezone
14+
Timezone::set('Europe/Brussels');
15+
16+
// Display dates stored as UTC in the app's timezone:
17+
// (database value: 2022-12-13 09:00:00)
18+
echo $model->occurred_at->format('d.m.Y H:i'); // Output: 13.12.2022 10:00
19+
20+
// Store dates using automatic UTC conversion:
21+
$model->occurred_at = '2022-12-13 20:00:00';
22+
$model->save(); // Database value: 2022-12-13 19:00:00
23+
```
24+
725
## Getting started
826

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

0 commit comments

Comments
 (0)