frik Posted February 8, 2015 Share Posted February 8, 2015 Hi folks, My web server is (I think) in Europe. My scripts that run on it don't currently know automatically what the locality is of the user who visits the website. How can I make sure that my PHP code displays the right time for the user? Also, I store time/date in my database using GMT unix time. How can I always convert that to a string that is localized to my visitors? Thanks. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted February 8, 2015 Share Posted February 8, 2015 You want to deal clientside with this, javascript is what you want. http://thisbythem.com/blog/clientside-timezone-detection/ 1 Quote Link to comment Share on other sites More sharing options...
frik Posted February 8, 2015 Author Share Posted February 8, 2015 You want to deal clientside with this, javascript is what you want. http://thisbythem.com/blog/clientside-timezone-detection/ Hello, my website is free of Javascript at the moment. I personally use NoScript most of the time. Isn't there a way to get the timezone from perhaps some data that the browser sends in the header? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted February 8, 2015 Share Posted February 8, 2015 need something client side to interact with a users browser Quote Link to comment Share on other sites More sharing options...
kicken Posted February 8, 2015 Share Posted February 8, 2015 There is nothing in a standard HTTP request that will tell you what the user's timezone is. You either need to ask them for it then save it somewhere, or you need to use Javascript to query for it from the browser. I tend to just output all dates and times in UTC and then on page load use Javascript to convert them all into the user's local timezone. If they have JS enabled, they will see the proper time. If they don't, they will still see the UTC time at least. If you also display the current time somewhere on the page, they will be able to see what the difference is between the time displayed and their local time. Quote Link to comment Share on other sites More sharing options...
frik Posted February 8, 2015 Author Share Posted February 8, 2015 There is nothing in a standard HTTP request that will tell you what the user's timezone is. You either need to ask them for it then save it somewhere, or you need to use Javascript to query for it from the browser. I tend to just output all dates and times in UTC and then on page load use Javascript to convert them all into the user's local timezone. If they have JS enabled, they will see the proper time. If they don't, they will still see the UTC time at least. If you also display the current time somewhere on the page, they will be able to see what the difference is between the time displayed and their local time. Is there a simple JS script that will do this conversion, or would I need to import a bunch of code? Also what about services that identify time zone based on IP? Quote Link to comment Share on other sites More sharing options...
kicken Posted February 8, 2015 Share Posted February 8, 2015 Is there a simple JS script that will do this conversion, or would I need to import a bunch of code? If you only care about the local timezone and you can output a unix timestamp then it's relatively simple. Example fiddle var timestamp = 1423419743; //Have PHP provide this value. var dt = new Date(timestamp*1000); var formatted = (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds(); console.log(formatted); With a little extra work you could remove the need to output the timestamp and use the date string instead:Example Fiddle var datetimestr = '2015-02-08 18:22:23'; //Have PHP provide this value somehow var matches=datetimestr.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2})\d{2})\d{2})/); var dt = new Date(); dt.setUTCFullYear(matches[1]); dt.setUTCMonth(matches[2]-1); dt.setUTCDate(matches[3]); dt.setUTCHours(matches[4]); dt.setUTCMinutes(matches[5]); dt.setUTCSeconds(matches[6]); var formatted = (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds(); console.log(formatted); You'll probably want to make formatted a little nicer, but it's generally not a lot of work/code. If you want to get more elaborate and be able to do easier formatting, multiple timezones, etc then you'd want to look into a library such as moment.js and moment-timezone.js Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.