Set Timezone in laravel or get current time based on different timezones
Today we will learn how to set timezone globally in laravel and also we will learn how to get time of any timezone.
Laravel: Change Timezone globally
Go to YOUR_PROJECT/config/app.php and open app.php and scroll to timezone section (line number 70) then you can see code like below
'timezone' => 'UTC',
replace it with any timezone that you want to apply.To view or search a timezone you can check this PHP official documents. For example i want to change timezone to America/Vancouver so after changes my code is looking like below.
'timezone' => 'America/Vancouver',
Now save this and print current time using date('Y-m-d H:i:s") function.
Laravel: Get Current time based on any timezone
Here we will use Carbon to get current time. So copy the below code and run it and you will see current time based on timezone you have passed.
$currentTime= Carbon::now('America/Vancouver');
return $time = $currentTime->toTimeString();
Thank you for reading :)
Comments
Post a Comment