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

Thread: default timezone

  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
    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.

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

    Default

    We are in the same boat.

    I was also looking for a way to set a domains timezone through CPanel because server logs are also annoying with the timestamps written to system time. Or possiblely through some Apache directive that could be added to htaccess file.

    I am going to check further on the Apache thing and post back results.

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

    Default

    Well you can set your env variable using an Apache directive. Place this line in your .htaccess file in your document root directory:

    SetEnv TZ America/Chicago

    After placing this in my .htaccess file, PHP works like a champ and you no longer have to make the adjustment in your PHP code. However, MySQL still requires the per connection manipulation.

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

    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 09: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 |

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