Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: default timezone

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Scott's Avatar
    Scott is offline Certified Glow Sage
    Join Date
    Oct 2006
    Posts
    27

    Default default timezone

    what is the best way to set the default timezone for php scripts?

    I have attempted:
    PHP Code:
    ini_set("date.timezone","America/Chicago"); 
    but that doesn't seem to do the trick.

    Any suggestions would be greatly appreciated.

  2. #2
    Dmitriy is offline Nearly a Master Glow Jedi
    Join Date
    Feb 2007
    Location
    Ukraine
    Posts
    124

    Default

    Hey, Scott

    What PHP version we're talking about? I presume PHP5...

    You can try also using
    bool date_default_timezone_set ( string $timezone_identifier )

    Timezones list: PHP: List of timezones in the group America - Manual
    Documentation on your issue: PHP: date_default_timezone_set - Manual

    However, I'd suggest saving all date\time value in UTC & than add timezone offset before the output. This is better than to write to users from Japan that all times are UTC -6 or something like that.

    Let me know how it goes

  3. #3
    Scott's Avatar
    Scott is offline Certified Glow Sage
    Join Date
    Oct 2006
    Posts
    27

    Default

    Alright, I got the php part to work but I am now having trouble with MySQL now().

    Here is the code that I have so far:

    PHP Code:
    echo "<p>PHP 1: ".date("Y-m-d h:i:s")."</p>"// outputs system time, ok

    $q "select now()";
    $r mysql_query($q);
    $d mysql_fetch_assoc($r);
    echo 
    "<p>MySQL 1: ".$d['now()']."</p>"// outputs system time, ok

    putenv("TZ=America/Chicago"); // change php timezone
    echo "<p>PHP 2: ".date("Y-m-d h:i:s")."</p>"// outputs my timezone, ok

    $q "select now()";
    $r mysql_query($q);
    $d mysql_fetch_assoc($r);
    echo 
    "<p>MySQL 2: ".$d['now()']."</p>"// outputs system time, didn't expect this to work for my timezone but why not try

    $q "set time_zone = '-5:00'"// change mysql timezone
    $r mysql_query($q);

    $q "select now()";
    $r mysql_query($q);
    $d mysql_fetch_assoc($r);
    echo 
    "<p>MySQL 3: ".$d['now()']."</p>"// outputs my timezone, ok 
    Per the MySQL docs, I should be able to tell MySQL the timezone that I am in by name, so instead of writing:
    PHP Code:
    $q "set time_zone = '-5:00'"
    I should be able to write:

    PHP Code:
    $q "set time_zone = 'America/Chicago'"
    or:

    PHP Code:
    $q "set time_zone = 'US/Central'"
    However, both of those attempts when using names fail and MySQL complains about them.

    Per MySQL doc:
    Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.

    Can we get these tables loaded or is there another solution to get now() to work as expected?

  4. #4
    Scott's Avatar
    Scott is offline Certified Glow Sage
    Join Date
    Oct 2006
    Posts
    27

    Default

    No takers?
    Am I the only one with this issue?
    What are others doing to work around this?

  5. #5
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    I just use #'s. I only have to report one or two timezones, so I have no need for this sort of complication, so Im not a taker, sorry.

    Here are the tables.
    MySQL :: Time zone description tables

    You can open a supprt ticket and give that link then make a request to have it installed in the mysql DB.

  6. #6
    Scott's Avatar
    Scott is offline Certified Glow Sage
    Join Date
    Oct 2006
    Posts
    27

    Default

    So you do this ...

    "set time_zone = '-5:00'"

    How do you handle daylight savings time?

  7. #7
    Matt's Avatar
    Matt is offline GlowHost Administrator
    Join Date
    Jan 2005
    Location
    Behind your monitor
    Posts
    6,266

    Default

    Quote Originally Posted by Scott View Post
    How do you handle daylight savings time?
    The scripts I have seen, have a checkbox which adjusts the script settings/clock by one hour when it is checked, or not checked respectively. But really, if PHP is using the server time then the time should always be right since that clock is updated by rdate.

    As for MySQL, I have no idea, if anyone finds it, I'd be interested in it for a script I am working on as well.
    Last edited by Matt; 03-13-2008 at 08:05 PM.
    Send your friends and site visitors to GlowHost and get $125 plus bonus!
    GlowHost Affiliate Program | Read our Blog | Follow us on X |

  8. #8
    Scott's Avatar
    Scott is offline Certified Glow Sage
    Join Date
    Oct 2006
    Posts
    27

    Default

    After discussing this further off-line, I have come to the conclusion that I believe it would always be best to handle any recording of time into MySQL as GMT. Then to simply let PHP handle the displaying of time into your intended time zone.

    This would allow you to change the time zone domain wide by using the Apache directive. Then as Matt has pointed out and you have visitors that require the time to be displayed differently, you can override the default time zone set by Apache with PHP.

    As Mysql is concerned you can then make the adjustment on a per connection basis by setting the time zone to "0".

    I believe this would also aide in making your code more portable.

    This is my conclusion, any other thoughts or holes in my train of thought here?

  9. #9
    Matt's Avatar
    Matt is offline GlowHost Administrator
    Join Date
    Jan 2005
    Location
    Behind your monitor
    Posts
    6,266

    Default

    I think I too have heard that the way to do it is in fact, GMT. It does ring a bell anyways.

    The only portability problem I see is that not everyone uses Apache webserver.

    I suppose it would not much matter for you unless you plan on making this application for sale or distribution, and then, I still wonder. I think the more you can keep out of .htaccess the better though, because different web servers either do not read that file, or may interpret it differently than Apache does.

    Why not let PHP do all the time processing based on GMT in MySQL?
    Last edited by Matt; 03-14-2008 at 10:29 AM.
    Send your friends and site visitors to GlowHost and get $125 plus bonus!
    GlowHost Affiliate Program | Read our Blog | Follow us on X |

  10. #10
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    I don't. Which is probably why one of my programmers had issues this weekend. I have always used PHP for the time, and it wasn't until this weekend that I got a sense of the ugliness of the fact that Mysql doesn't seem to get the time from the same place PHP does.
    He handles my auction launcher for eBay, and many users had their scheduled auctions go out an hour late. Ughh....

    I have been meaning to look further into this, and this may be the time.

    But even if the tables get populated, I came accross indications that the tables would need to be constantly updated for that very reason. I didnt read hard enough or look at the structure to see how it handles that.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14