Laravel Tutorials

Learn how to manage timezones in your Laravel Apps

Published

In this video, Ben Holmen teaches us how to manage timezones in your Laravel apps. Some of the highlights include:

  • Store all dates and times in UTC
  • Define a default application Timezone
  • Use Carbon macros for conversions
  • Either automatically or manually, allow the user to select their timezone

Here are the code samples used in the video:

In Application Timezone Macro

Carbon::macro('inApplicationTimezone', function() {
return $this->tz(config('app.timezone_display'));
});

In User Timezone Macro

Carbon::macro('inUserTimezone', function() {
return $this->tz(auth()->user()?->timezone ?? config('app.timezone_display'));
});

Give it a watch and learn all about Timezones and how to easily manage them!

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Validate and Convert HEIC Images in Laravel image

Validate and Convert HEIC Images in Laravel

Read article
Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues image

Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues

Read article
Reject Unexpected Array Keys with Laravel Validation image

Reject Unexpected Array Keys with Laravel Validation

Read article
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article
Laravel Boost Project Rules: Teach Agents Your Conventions image

Laravel Boost Project Rules: Teach Agents Your Conventions

Read article
Extract an Image's Dominant Color in Laravel image

Extract an Image's Dominant Color in Laravel

Read article