I would like to print the time in local time in Laravel. If the user create a post it will display the created time on the server. How can I display it in local time ?
Try this method, I think this is what you want:
$localTime = $object->created_at->timezone($this->auth->user()->timezone);
Here, $this->auth->user()->timezone
will return current user's timezone, and timezone()
will convert created_at
to to user's local time.
If you want to get all visitors timezone (not just logged in users), you can you package for Laravel similar to laravel-geoip. It will generate $visitor['timezone']
for you which you can use like this:
$localTime = $object->created_at->timezone($visitor['timezone']);