Search code examples
laravellaravel-8php-carbon

How to get time in particular timezone in Laravel?


I am unable to get the time for a particular timezone using Laravel 8. I have tried the following:

Carbon::now(new \DateTimeZone('Indian/Mauritius'))

I have also tried to modify the timezone in file config\app.php as follows:

'timezone' => 'Indian/Mauritius'

It is always returning me the UTC time. How to get the time using Carbon in that specific timezone?

Edits: issue solved, the problem was because of a serialization issue, check here


Solution

  • This works for me:

    $dateNow = \Carbon\Carbon::now();
    $dateNow->setTimezone('Indian/Mauritius');