+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Multiple date & time support

  1. #1
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Multiple date & time support

    I will need multiple date & time support.

    if one user login in UK - then that's time UK time display.

    other one US then that's time US time zone display.

    another one AU then that's user AU time zone display.

    How it's handle using PHP.........

    Cheers
    Asif Khan
    http://www.phpasks.com
    Asif Khalyani
    http://www.phpasks.com

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Multiple date & time support

    You just need to store the GMT (or any other) time as a UNIX timestamp and add/remove a certain amount depending on the timezone offset.
    For example, a timestamp is 1000000 (GMT). If a user in timezone GMT needs the date, you don't have to change it and just use the date() function on it. If a user in GMT+1 requests it, you add 60*60 (60 minutes * 60 seconds = 1h) to it (= 1003600) and use the date() function on that.

    - Marshian
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: Multiple date & time support

    Can you give me any example for that code????


    Quote Originally Posted by marshian View Post
    You just need to store the GMT (or any other) time as a UNIX timestamp and add/remove a certain amount depending on the timezone offset.
    For example, a timestamp is 1000000 (GMT). If a user in timezone GMT needs the date, you don't have to change it and just use the date() function on it. If a user in GMT+1 requests it, you add 60*60 (60 minutes * 60 seconds = 1h) to it (= 1003600) and use the date() function on that.

    - Marshian
    Asif Khalyani
    http://www.phpasks.com

  4. #4
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: Multiple date & time support

    Alternatively you can get a user to set their own timezone upon registration, and then fetch it from a database. I would suggest making just a tinyint field in your database, and then having an array of timezones. Check out the date_default_timezone_set() function. And for a list of timezones; try this.
    - When in doubt, refer to the PHP manual.

  5. #5
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Multiple date & time support

    An example:

    PHP Code:
    $result mysql_fetch_array(mysql_query("some query here"));
    $time $result["time"]; //Unix timestamp, we suppose it's 1000000
    $result mysql_fetch_array(mysql_query("some query here"));
    $usertimezone $result["timezone"]; //We'll suppose this is -10 (= GMT-10)
    $localisedtime $time $usertimezone*60*60//1000000+(-10)*60*60
    echo date("date format"$localisedtime); 
    - Marshian
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  6. #6
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: Multiple date & time support

    Thanks, Marshian

    regards, Asif

    Quote Originally Posted by marshian View Post
    An example:

    PHP Code:
    $result mysql_fetch_array(mysql_query("some query here"));
    $time $result["time"]; //Unix timestamp, we suppose it's 1000000
    $result mysql_fetch_array(mysql_query("some query here"));
    $usertimezone $result["timezone"]; //We'll suppose this is -10 (= GMT-10)
    $localisedtime $time $usertimezone*60*60//1000000+(-10)*60*60
    echo date("date format"$localisedtime); 
    - Marshian
    Asif Khalyani
    http://www.phpasks.com

  7. #7
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Multiple date & time support

    I have a quick daft question on this..

    According to the above posts, I could use date_default_timezone set, but I've been using the putenv() function.

    i.e. putenv ("TZ=".$_SESSION['MM_UTZ']);

    where $_SESSION['MM_UTZ'] is the user's timezone preference.

    This automatically works out the difference in Timezones with Timestamps (e.g. Time()) when echoing dates.


    What exactly is the difference between the two?

    Also, I still haven't managed to find a good solution for date inputs...

  8. #8
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Multiple date & time support

    Quote Originally Posted by scopey View Post
    Alternatively you can get a user to set their own timezone upon registration, and then fetch it from a database. I would suggest making just a tinyint field in your database, and then having an array of timezones. Check out the date_default_timezone_set() function. And for a list of timezones; try this.
    Thanks Scopey

    I have attached 2 files - one is the insert page and the other is a test time page, which contains the $ndttime= new DateTime($timefield, new DateTimeZone($utz)); function.

    This subject seems to get more and more complex the more I get into it!

    I hope you can help and will offer 100 credits to anyone who provides a conclusive solution.
    Attached Files
    Last edited by freecrm; 08-30-2008 at 03:48 PM.

  9. #9
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: Multiple date & time support

    I'm not completely sure what you're trying to do, but I made a quick example php page to show what I meant about making an array of timestamps and setting a session variable for your timestamp.

    Here's the code here:

    PHP Code:
    <?php
    session_start
    ();
    if(
    sizeof($_POST) > && $_POST['submit']){
        
    $_SESSION['timezone'] = $_POST['timezone'];
    }
    //I can only be bothered inserting a few timezones...
    $timezones = array(
        array(
    'stamp'=>"US/Eastern",'name'=>"United States - Eastern"),
        array(
    'stamp'=>"US/Central",'name'=>"United States - Central"),
        array(
    'stamp'=>"NZ",'name'=>"New Zealand"),
        array(
    'stamp'=>"Europe/London",'name'=>"United Kingdom")
    );
    if(isset(
    $_SESSION['timezone'])){
        
    date_default_timezone_set($timezones[$_SESSION['timezone']]['stamp']);
    }


    print 
    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>'
    ;
    print 
    date('l \\t\h\e jS \of F Y')."<br />".date('g\:ia')."<br /><br />";
    print
    "<form action=\"".$_SERVER['REQUEST_URI']."\" method=\"post\">";
    print 
    "<select name='timezone'>";
    for(
    $i=0;$i<sizeof($timezones);$i++){
        print 
    "<option value='{$i}'>{$timezones[$i]['name']}</option>";
    }
    print 
    "</select>";

    print 
    '<input type="submit" name="submit" value="Set timezone" />
    </form>
    </body>
    </html>'
    ;
    ?>
    I havn't had time to test it as Absolut is down -.- . But all you need to do is finish and put that $timezone definition in a global include file and then you can call date() like you normally would, and the timezone will be set for that particular user.

    [EDIT] -> I have now tested it... Works fine. Look here for the script in action!
    Last edited by scopey; 08-30-2008 at 09:56 PM. Reason: Thought I might throw in UK, for freeCRM
    - When in doubt, refer to the PHP manual.

  10. #10
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Multiple date & time support

    Thanks Scopey, but I already have the timezones set from user preferences with this bit of code about half way down the page of inserteventphp.

    session_start();
    //set page timezone
    if ($_SESSION['MM_UTZ'] == NULL){
    $_SESSION['MM_UTZ']="UTC";}
    putenv ("TZ=".$_SESSION['MM_UTZ']);
    //set page timeformat
    if ($_SESSION['MM_UTF'] == NULL){
    $_SESSION['MM_UTF']="Y-m-d H:i:s";
    }

    This is fine for echos from the DB with timestamp format but my problem is getting a date into it!

    i.e.

    form field + JS date/time picker

    date/time picker returns a formatted date in Y-m-d H:i:s.

    the new datetime object needs to take this $_POST value and convert into a timestamp (numeric) string, associated with the users timezone.

    This is stored in the table in numeric format.

    When this is echo'd from the DB, it translates the numeric object and returns a date/time object relative to the browser.

    For instance, if user 1 in GB inserts a date/time (e.g. 2008-08-31 12:00:00), the new datetime object will enter a unix date: 1220180400 (GB)

    When user 2 in EST-6 echos this value, it will take into account the time difference and return a date/time value 2008-08-31 06:00:00.

    My problem is getting the new datetime object intot he database (ref the insert script).

    Is this any clearer?

    I have a page that tests this at http://www.freecrm.x10hosting.com/timetest.php
    Last edited by freecrm; 08-31-2008 at 10:44 AM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Offering SMF Support
    By bigguy in forum Ads & Offers
    Replies: 6
    Last Post: 03-08-2008, 01:28 PM
  2. More time to create website
    By -Daniel in forum Feedback and Suggestions
    Replies: 6
    Last Post: 07-03-2005, 04:34 PM
  3. digital.exofire.net | Support | problem#1 = " cPanel "
    By kaliforna in forum Free Hosting
    Replies: 10
    Last Post: 04-12-2005, 11:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers