Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automatically Converting Date and Time by Timezone

    I have been searching the Internet for hours trying to find a way to convert the local server time to a user's local time. I have seen quite a few ways of doing it. I am wondering if there was a simple way. I already know of the "adding/subtracting x hours to local time" based upon the GMT offset. However, that does not compensate for users in timezones in daylight savings time. In addition, this does not compensate for the date change, that is, if the server time is 2359 on Jan 1, a user in a timezone two hours ahead would actually be 0159 Jan 2.

    Any help would be appreciated.

  • #2
    New Coder
    Join Date
    Nov 2005
    Posts
    97
    Thanks
    1
    Thanked 0 Times in 0 Posts
    compensating for the date change.
    if you know the offset:

    date("dM'y H:i", strtotime("+2 Hours"));
    Edit: Nothing that can be considered offensive in the signature, please. // liorean

    www.moresense.co.uk

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,107
    Thanks
    2
    Thanked 326 Times in 318 Posts
    Here is some code that I did based on the FAQ example (sending variables from javascript to PHP) in the PHP manual. This sends the visitors date to the PHP page and could be modified to send any value that is available -
    PHP Code:
    <?php 
    if (isset($_GET['umm']) AND isset($_GET['udd'])) { 
      
    // output the value gotten from the browser as a test 
      
    echo "Your MM: {$_GET['umm']} Your DD: {$_GET['udd']}<br />\n"
    } else { 
      
    // form and pass the date variables 

      
    echo "<script language='javascript'>\n"
      echo 
    "var d=new Date()\n"
      echo 
    "var dd=d.getDate()\n"
      echo 
    "var mm=d.getMonth()\n"
      echo 
    "  location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}" 
                
    .  "&umm=\" + mm + \"&udd=\" + dd;\n"
      echo 
    "</script>\n"
      exit(); 

    ?>
    Also, here is a link to a post that basically sends the server's time to the browser and lets it calculate and use the difference between it and the visitor's time - http://www.codingforums.com/showthread.php?t=98742
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New Coder
    Join Date
    Feb 2004
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have a similar problem, although SQL is involved. I store a variable called date ($date) with other info. via a form

    When I query the database,
    Code:
    $query = "SELECT rain,f_name,l_name,property, town, date  FROM $table ORDER BY date DESC, rain";
    $result = mysql_query($query);
    $num_results = mysql_num_rows($result);
    As I try to output this to a table, there is a time lage between my local time and server time.
    Code:
    echo "<td class='header'>".date("d-m-Y", strtotime($row['date']))."</td>\n";
    How can I modify this to refelect my local time?
    BTW - date is kind of a default value - I didn't get them to add it, just to save time in form entry. Should I make them add the date - would that make the process easier?


  •  

    Posting Permissions

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