Is there a way to set local timezone in laravel?
In config/app.php
'timezone' => 'UTC',
After some research, stumbled upon the following PHP way of dealing with it:
$userTimezone = Auth::user()->timezone;
date_default_timezone_set($userTimezone);
// and change the configuration so they match
Config::set('app.timezone', $userTimezone)
But, Is there an elegant solution for this other than converting the timezone using the above code.
Cheers
Laravel Timezone is a package by James Mills that sets a timezone for a user in your application and then show date/times to them in their local timezone. It works by listening for the user login event and setting the timezone in the database. It uses Laravel GeoIP to look up the user using an IP address.
Don't do that. Never set the global Laravel timezone to user's timezone. You'll face endless problems.
Choose you application timezone, set the Laravel config to that timezone and never change it.
Instead, when you need to show a date in user's timezone do something like
User::find(1)->foobazed_at->timezone(Auth::user()->timezone);
To control which model columns are automatically converted to Carbon\Carbon
instances, add the following method to your model file:
public function getDates()
{
return array('foobazed_at', static::CREATED_AT, static::UPDATED_AT, static::DELETED_AT);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With