HOME       >>       Programming

Php Time Difference how to handle different time zones


sonesay

I'm trying to figure out how to deal with time entries in my mysql so it can be displayed correctly on my end. On my local server its not a problem as the time created is in line with my current time zone. The problem is when I use my scripts on Xisto server the time that gets created is the time on that server so its off.

 

I haven't really experiment on ways to handle this but I've seen some articles related to his in the past but didn't bother to read them up. I want to know if theres anyone who's dealt this with problem before and what solution did they use to get around it.

 

I'm storing my dates as 'year-month-day hour-minute-seconds' in mysql. If this is wrong then how am I suppose to be storing my dates and time. I use it for my creation and modify dates for my news article entries and portfolio item entries. I have a section on my page where I also log visiters so its displayed for me to view easily instead of going into cpanel.

 

 

The log shows your current date of 29th February while on my end its already 1st march.

 

Any insight would be greatly appreciated.


jlhaslip

I think the answer to your question will lie in using GMT as a time base and then adjusting according to your preferred Timezone.
PHP includes several time functions including gmmktime()
http://ca3.php.net/manual/en/function.gmmktime.php

Browse that link to see several suggested solutions to International timezone problems.

When in doubt, refer to the php.net site for a solution. very likely that someone else has had the same or similar problem in the past and many folks post their solutions to the Discussion board there. It is always my first source for answers regarding server-side scripting.


sonesay

Thanks for the link, Yeah I suspected it had to be done that way. I've actually been here before but as I was getting into reading up how to handle this issue and starting getting exposed to some of the terms and lost in it all I gave up as it was not a big a deal to me then. Now I have meet my nemesis once again and I have time and enough determination to try and win this round. Yeah all that technical terms and jargon like GMT, UTC Unix timestamps goes right over my head but I'll keep at it. As nice as it is to get spoon feed a code and example with step by step instructions on how to do it I'll keep at it and see if I can figure it out. How can you call your self a web developer if you cant even figure out some time issues right? Another skill set to put under your belt I guess once you learn it.Thanks again for info and I'll post any updates if/when I figure it out.Update: After messing around with it for a bit I think I get it now, My local time is about 13 hours ahead of the unix time stamp or what ever time stamp its called. So I basically add it and I get my local time but this brings me to another question on how to properly handle it when users are from all over the whole and are in different time zones. How would you be able to figure out what time zones they are in to appropriately apply the offset for them?


jlhaslip

All Servers have a time setting inside them somewhere in the vastness of the Server. The time setting is whatever you want it to be. It might be set to GMT or whatever the Local time is in the timezone the Server is located at. It really doesn't matter if the Server is set to GMT or not because the format of the Server Time is always in a format which can calculate the GMT time, so GMT is a calculated value on all Servers.

 

Most Forums I belong to *gasp* (I go to a few) *gasp* have a User Profile page and the Profile page includes a drop-down for selecting my time zone. I set my time-zone and the forum software adds the time-zone difference to all of the times that it displays for me.

 

It does that by calculating GMT based on the Server's Time Settings, so when I browse to the Xisto, the IPB software includes a series of processes for providing me information. One of the processes goes to the server, fetches the Server time, then goes to the database and gets some information which includes the time I posted this reply. It has a time of the posting, usually in GMT time. Then the software grabs the time-offset for me from my profile and adds the two together before it displays the time of posting to me. Simple enough, really.

 

The key to the whole issue is basing the time on GMT which is a Standard and easily referenced as per the above pseudo-code explanation. Hope this clears things up for you...


sonesay

Yeah so the only way to determine a users GMT off set is for them to choose one like in their profile. This means they must be signed up and have their settings on your database. I do not currently use a system for my current site theres no way I'll be able to determine each users GMT time offset.Is there any other way to determine what timezone they are in from their IP or what ever, If you didn't have them register on the site like in the above situation. I'm guessing its going to be difficult to obtain this. Probably too much trouble then its worth.Update:I been messing around and reading a bit on the PHP.net on dates and times. There is some good info there too, most examples I've seen people have been writing functions that take in the unix timestamps and output a GMT taking parameters ($sign, $hours, $daylightsavings) not in that particular names but you get the idea. This works well if you know the users time zone they are in and if daylight savings applies for them. i.e membership details just like how we have on Xisto forums.I cant have that on my site and even if I did I doubt users would even sign up. I still would like to be able to get the same effect like displaying the correct time for each user no matter where they are in the world when I'm outputting my news articles. I was thinking one way would probably have a drop down menu with the GMT offset on there where users can pick but that would require page reload to take effect not to mention annoying. also another problem would be to determine if day light savings did apply for them as this can vary depending on where you are from. Just those two problems I still need to work out. Unless I just let it be and display time in NZL only but thats too easy.


jlhaslip

javascript could add the difference between the Server generated time and the 'local' time. I don't know enough javascript to do that myself, but I'll search around while I travel through a couple of places.



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.