How to convert server time to local time in Laravel?

前端 未结 8 1540
失恋的感觉
失恋的感觉 2020-12-30 14:50

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 ?

8条回答
  •  清歌不尽
    2020-12-30 15:47

    You can do this by using javascript. Use following libraries:

    1. moment.min.js (http://momentjs.com/downloads/moment.js)
    2. moment-timezone-with-data.js ()
    3. jstz.min.js (https://bitbucket.org/pellepim/jstimezonedetect)

    Here is the code :

    var update_at = 'updated_at;?>'; //set js variable for updated_at
    var serverTimezone = 'YOUR SERVER TIME ZONE'; //set js variable for server timezone
    var momentJsTimeObj = moment.tz(update_at, serverTimezone); //create moment js time object for server time
    var localTimeZone = jstz.determine(); //this will fetch user's timezone
    var localTime = momentJsTimeObj.clone().tz(localTimeZone.name()).format(); //convert server time to local time of user
    

    Now you can display local time through js

提交回复
热议问题