SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Timezone

  1. #1
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Timezone

    hi

    How can we create different time zones using php5

    (I mean my server in Uk ,But i am working in US.I want US time in my place and uk time in UK,In nutshell each country should automaticaly show its own time in the wbpage using php5)

    is it possible in php5?

    please help me

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In a central configuration file (that every script calls) I would use date_default_timezone_set with a value of Europe/London
    Then whenever you insert data into the DB use PHP functions to determine the time. This is to store them at GMT +0.

    Then when formatting the time for display use the PHP date/time functions to translate into a given users local time. Namely date and strtotime.

    I'm assuming London in in the same timezone as Grenich.
    Last edited by cranial-bore; May 16, 2007 at 04:11.

  3. #3
    SitePoint Addict aniltc's Avatar
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    399
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any examples? i am not able to find an example in php documentation

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    //Config File
    date_default_timezone_set('Europe/London');


    //When inserting data (for DATETIME field)
    $date date('Y-m-d H:i:s');
    $query "INSERT INTO `table` (date) VALUES ('$date')";


    //When selecting data
    $query "SELECT date FROM `table` WHERE ID = 1";
    $dateFromDB ''//use mysql_query or equivilent


    //Transform date to users timezone offset. Must be configured and selected elswhere
    //Assumes offset is decimal hour representation. eg. +10, -5.5
    $date date('d M Y g:i:s'strtotime($dateFromDB)+$offset*3600);

    echo 
    "Some event happened at $date (your time), but in our DB its stored as $dateFromDB";

    ?>
    Basically all dates will be stored as GMT 0. You must then let users configure their timezone offset. Display it as hours and minutes (e.g +10:00 or -5:30) but store it somewhere as decimal values (hours and fractions of hours).

    Then when you select data from the DB you will convert the DATETIME string to an integer date using strtotime, apply the offset and format using the date function to output it however you want.

    My code outputs code as d M Y g:i:s but you can set those flags to whatever you want. See the manual for details.

    Only real shortcoming for this method is that it won't take users daylight savings into account, as you can't tell from their offset whether they observe DLS or not.


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
  •