nevynev Posted August 6, 2007 Share Posted August 6, 2007 Hi, I've spent hours going round in circles with this and really dont get it. I'd like some code which simply works out how many hours ahead/behind a users local time is to the time of the server (in London). It needs to take into account summer time +1 hrs etc. e.g. the code works out that a user is in Paris (or France) and then works out that they are +1 hr ahead of uk time. This would allow me to add an hour to times stored in databases when they are showed to the users, as the site is an international networking site. So any help is welcome. I had some help on IRC but we didnt get anywhere. So very lost in all of this! Thanks NevyNev Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/ Share on other sites More sharing options...
AndyB Posted August 6, 2007 Share Posted August 6, 2007 Local time is local time. You could determine it via javascript, but js might be disabled by the viewer or they might even have their time set wrongly. Having everything stored with GMT times/dates enables you to know exactly when things happen since it's a common world standard and - it seems to me - that a common time standard is exactly what you need on an international site. If you attempt to adjust for local times, you'll end up with people supposedly viewing things before they happened (I'm 5 or 6 hours earlier than London time). And the UK isn't the only place with 'summer time'; and the dates for summer time vary from place to place. GMT is always right. Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316662 Share on other sites More sharing options...
Psycho Posted August 6, 2007 Share Posted August 6, 2007 PHP has no knowledge or access tot he user's time. You could do one of two things: 1) Assuming that user's have an account, some time during the registration process have the user select their current time to figure out the offset and store that in the database. 2) Use javascript to dynamically get the user's current time. Without knowing how you plan to use the data I don't have any idea how to best implement it. If you were to use it for for displaying a time in the future/past you could save the server time and the time to be displayed as a javascript variables. then use javascript to determine the correct offset based upon the server current time and then display the future time in the user's timezone. Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316663 Share on other sites More sharing options...
nevynev Posted August 6, 2007 Author Share Posted August 6, 2007 I think ill stick to php/mysql as Andy B stated js could be off. When users sign up they have to select their location - Country & City. The countries and cities are stored in mysql database and I got the data from: Info: http://dev.mysql.com/doc/world-setup/en/world-setup.html Download the .sql file - http://dev.mysql.com/doc/ It's great as I have a list of all the worlds cities and countries now but the cities table annoyingly doesnt have the timezone shown. seeing as there are over 4000 cities listed I don't really fancy inputting the time zone for each one!! That may give you a clearer idea of what I've currently got. Thanks for the help so far NevyNev Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316673 Share on other sites More sharing options...
AndyB Posted August 6, 2007 Share Posted August 6, 2007 On a trivial basis, local time zones are dependent on longitude although national and/or geographic boundaries mess up that nice little idea. Your database has longitude for the cities so you can easily upgrade it to add a TZ field that will be correct some of the time (which is better than never), without actually entering the data. 'Summer' time will always be a problem for you, and so will countries like China where there's one time zone (Beijing time - GMT+ for a country that you would expect to have many TZs. http://www.travel.com.hk/region/timezone.htm Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316676 Share on other sites More sharing options...
nevynev Posted August 6, 2007 Author Share Posted August 6, 2007 How would sites like facebook or bebo do this? I am getting so confused and stressed over this. Thanks NevyNev Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316784 Share on other sites More sharing options...
AndyB Posted August 6, 2007 Share Posted August 6, 2007 How would sites like facebook or bebo do this? best guess - timezones in a database I am getting so confused and stressed over this. Explain specifically what you're confused about. It all seems pretty straightforward to me. I've told you how to guesstimate timezone offsets based on longitude and - without entering a bunch of stuff manually - that's as good as it's going to get. Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316802 Share on other sites More sharing options...
Psycho Posted August 6, 2007 Share Posted August 6, 2007 I understand your frustration, but considering that each country can pretty much do what they damn well please, thre is no "easy" solution. But, another thing to keep in mind is that many countries in Europe would have one timezone for the entire country. You could find the timezone offset and start/end dates for summer time for a country and add it to the country table (or do an update on the city table for records associated with that country). That would greatly reduce the number of individual entries you would need to make. Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-316828 Share on other sites More sharing options...
nevynev Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks for the advice. I've decided to go for the "this message was posted 2 days ago" hours/weeks/months/years so no exact times are required - that's how bebo does it. For Facebook they seem to set the timezone for each network - and they've probably got a fairly large work force! I'll leave timezones alone - if my client wants to pay for me to do it at a later date they can! Thanks for the help. A somewhat more chilled out NevyNev Quote Link to comment https://forums.phpfreaks.com/topic/63539-difference-in-hours-between-server-time-london-and-users-location/#findComment-317784 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.