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 is the creator of Laravel News and has been covering Laravel since 2012.