SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Timezone problem

    Hey,

    I have some problem with timezones in php because my server is not in the same zone as I am. I am GMT+1, and my server is -5 I think.

    So, this is the code:

    date("Y-m-d H:i");

    Anybody with an idea how to adjust timezone to +1?

    Thank you,
    Ante

  2. #2
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using PHP5 or PHP4?

    With PHP5 there are actually functions to set the timezone. But with PHP4, use gmdate() and subtract the necessary number of seconds from the timestamp passed to it. 60*60*$num_hours

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The easiest way is to pass a timestamp to the date() function, through PHP4 and PHP5. Normally, you'd do date("Y-m-d H:i",time()) and the second parameter defaults to time() anyway. If you want to adjust from -5 to +1, you need to add six hours, so time() + (60 * 60 * 6) - 60 seconds, 60 minutes, six hours. date("Y-m-d H:i",time() + (60*60*6)) should do the trick.

  4. #4
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's it. Thank you both!

  5. #5
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ante View Post
    Hey,

    I have some problem with timezones in php because my server is not in the same zone as I am. I am GMT+1, and my server is -5 I think.

    So, this is the code:

    date("Y-m-d H:i");

    Anybody with an idea how to adjust timezone to +1?

    Thank you,
    Ante
    I'm based in Melbourne but the webhost I use is in California.. to set my web apps to melbourne timezone I use a general include file which is included at the top of every file..

    in php 4 you would do this -
    PHP Code:
    // set php to melbourne timezone
    $melb_zone '/usr/share/zoneinfo/Australia/Melbourne';
    putenv('TZ='.$melb_zone); 
    in PHP 5 do this -
    PHP Code:
    // set php timezone to Melbourne
    date_default_timezone_set("Australia/Melbourne"); 
    To set MySQL to run in Melbourne timezone I do this-
    PHP Code:
    // set mysql timezone to Melbourne
    $query "set time_zone='Australia/Melbourne'";
    mysql_query($query) or die ("Could not change database timezone\n"); 


Bookmarks

Posting Permissions

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