Nov 21, 2009

Php And Time Zones.....

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Php And Time Zones.....

Chesso
As of right now when a user replys on my forums I store the date/time in the thread table/row for display so users know when the last reply was made.

This looked ok for me on my computer for local testing using WAMP5 because of course my computer is set to my timezone, but I reliased after I had updated my site last night with the new feature and checked the site dismorning that it is using the timezone for Asta's server.

Iv'e done some searching and found this default_timezone_set() function for PHP but it doesn't tell me how to find what I should give as the paramater for my particular timezone (I'm in Sydney so GMT+10, EST, I think).

Anyone know how to use this function?

P.S. I use the date() function and just store it as a string in the mysql database.

Comment/Reply (w/o sign-up)

vujsa
QUOTE(Chesso @ Jul 31 2006, 08:35 PM) *
As of right now when a user replys on my forums I store the date/time in the thread table/row for display so users know when the last reply was made.

This looked ok for me on my computer for local testing using WAMP5 because of course my computer is set to my timezone, but I reliased after I had updated my site last night with the new feature and checked the site dismorning that it is using the timezone for Asta's server.

Iv'e done some searching and found this default_timezone_set() function for PHP but it doesn't tell me how to find what I should give as the paramater for my particular timezone (I'm in Sydney so GMT+10, EST, I think).

Anyone know how to use this function?

P.S. I use the date() function and just store it as a string in the mysql database.


Well, I think the best way to do what you want is to require the user to specify their timezone. Which will set the offset from UTC. If you save your dates as timestamps which is probably best since it will offer you more options in the future then you can simply add or sbtract the offset to get the users local time.

CODE
$user_tz = 5;
  $user_offset = $user_tz * 3600; // Number of hours X number of minutes X number of seconds
  $timestamp = time();
  $local_timestamp = $timestamp + $user_offset;


In this case, if the timestamp was 1154530597 then the local timestamp would be 1154548597 of 18000 seconds ahead.

Then use your date function as normal but instead of leaving the timestamp condition blank, specify it as $local_timestamp.

If the users timezone had been -5, the formula would still be the same since -5 * 3600 would be -18000. Then 1154530597 + -18000 would be 1154512597.

Now, you do have several options for the storing and retrieving of the users time offset.
You can either save the user's offset in the database as hours or seconds. If you save it a seconds then you only need to calculate it one time when you first save it or you can save it as hours and recalclate the number of seconds every time the user's offset is required.
When it comes time to retireve the offset, you can either request it from the database for each page and use it from there or you can request it once when a session is started and save it in the session variable like so:
CODE
session_start();
  if(!isset($_SESSION['user_offset'])){
      $_SESSION['user_offset'] = 18000; // You would  actually get this data from the database.
  }


There is one more thing to consider, Daylight Savings Time! mad.gif
During the summersome countries change their time to have more usable daylight in the evening instead of the early morning. In the northern hemisphere, this is from April 1st to October 1st and I guess it is the opposite in the southern hemisphere. As a result, during DST, you will need to subtract one hour (3600 seconds) from the user's offset if and only if they infact use the DST system where they live. I personally don't understand why anyone would change their time twice a year. It gets dark here (Indiana, US) right now (August 2nd) at about 9:30 PM. Really anoying cause I forget to eat until like 8:30 or 9:00 PM now.
You'll need an option for your user to select if they will be using DST in the summer.

I'm sorry I didn't discuss the date_default_timezone_set() function since it is new to PHP5 and we aren't using PHP5 on the AstaHost server yet.

Well, I hope this helps. cool.gif

vujsa

 

 

 


Comment/Reply (w/o sign-up)

TavoxPeru
QUOTE(Chesso @ Jul 31 2006, 07:35 PM) *

Iv'e done some searching and found this default_timezone_set() function for PHP but it doesn't tell me how to find what I should give as the paramater for my particular timezone (I'm in Sydney so GMT+10, EST, I think).

Anyone know how to use this function?

P.S. I use the date() function and just store it as a string in the mysql database.

Do you mean date_default_timezone_set() function??? well, if it is, you can't use this function with the Asta's server because this function only works with PHP 5+ and as you know the Asta's server works with PHP 4.4.2. Now, the parameter that this function use is a timezone identifier and for your case it is Australia/Sydney, you can find a complete list of timezone identifiers in the appendix H of the php manual.

Best regards,

Comment/Reply (w/o sign-up)

Chesso
So they do list the timezones somewhere? That's good to hear, I even googled it like crazy and couldn't find anything usefull.

Yeah I also found out about that function only working for PHP5+.

Comment/Reply (w/o sign-up)

vujsa
QUOTE(Chesso @ Aug 3 2006, 12:03 AM) *
So they do list the timezones somewhere? That's good to hear, I even googled it like crazy and couldn't find anything usefull.


Here is the site I use: http://www.worldtimezone.com/

Good Luck.

vujsa

Comment/Reply (w/o sign-up)

TavoxPeru
QUOTE(vujsa @ Aug 3 2006, 09:49 AM) *

Here is the site I use: http://www.worldtimezone.com/

Good Luck.

vujsa

Nice and helpful site, but the only thing i dont like about it is that the name of my country Perú does not appears mad.gif

Best regards,

Comment/Reply (w/o sign-up)

FirefoxRocks
The PHP site lists the time zones here: http://ca3.php.net/manual/en/timezones.php

Can anyone find me a list of those with their GMT offsets? It would be a pain to look up the GMT offsets of over 300 time zones in that list!

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Php Zones


    Looking for php, time, zones

See Also,

*SIMILAR VIDEOS*
Searching Video's for php, time, zones
advertisement



Php And Time Zones.....

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com