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 ?
In my blade file I used this code to display created time,
{{{ $posts->updated_at }}}
Which displays the time in database, which is a server time. How can I convert it to users local time ?
Best option is to do this with Javascript, get client's timezone and then convert server time to cleint's accordingly.
Please refer https://stackoverflow.com/a/1837243/4007628
Server time should stick with UTC timezone
In front end, you can use moment.js to render the correct local timezone. I tested this in moment version 2.22.2.
Simply add Z to the datetime value and moment will render the date to user's local time zone
I found a solution to convert time to local time by using session. The current time zone offset will store on session to calculate users time. Create a jquery post function to post users timezone offset to session. This is my code,
default.blade.php
routes.php
HomeController.php
Helpers/helper.php
index.blade.php
Now we can print the time in clients time zone. The time zone setting code run only when the session empty.
Try this:
Try this method, I think this is what you want:
Here,
$this->auth->user()->timezone
will return current user's timezone, andtimezone()
will convertcreated_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:Go to app.php page in Config folder and change this
to
reference https://laravel.com/docs/5.5/configuration
find your timezone http://php.net/manual/en/timezones.php