Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Set Timezone in laravel or get current time based on different timezones

 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 :)







This post first appeared on Thecoderain PHP,wordpress,cakephp,codeigniter,js, Jquery, Ajax Free Tutorials, please read the originial post: here

Share the post

Set Timezone in laravel or get current time based on different timezones

×

Subscribe to Thecoderain Php,wordpress,cakephp,codeigniter,js, Jquery, Ajax Free Tutorials

Get updates delivered right to your inbox!

Thank you for your subscription

×