简体   繁体   中英

Laravel Timezone doesn't convert to the correct timezone in Laravel

I am trying to build a simple function where I want to fetch a DateTime from database that is stored in UTC format and display it to the user in their local timezone. I found heaps of solutions that works, but the timezone is not correct.

 $time = Carbon::createFromFormat('Y-m-d H:i:s', $user->created_at)->settings(['timezone' => 'Australia/Sydney']);

So this code takes in $user->created_at, "created_at": "2021-12-02T21:18:31.000000Z", which in UTC format created on 21:18, which is 8:18 am in Sydney, yet, when I run the code i get a wrong time zone, "2021-12-02T10:27:29.000000Z", which is 10:27 am.

I am not sure whats happening. Any Help will be greatly appreciated

I have found a great answer!

From StackOverflow, https://stackoverflow.com/a/53496435/17264753

Happy Coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM