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 5 of 5
  1. #1
    mrx
    mrx is offline
    New Coder
    Join Date
    Oct 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how do i auto adjust time base on user local time

    how do i auto adjust time base on user local time

    i want to put in a time like 1:00pm us time manually, how can i make this number automaticly change to whaterver time equivalent in user local timezone


    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script>
    var time = "10:30 am";
    </script>
    </head>
    
    <body>
    <script>document.write(time)</script>
    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You can't as you have no way of knowing which time zone the user is in.

    I am in the UK and this forum's clock tells me the time is 11.15 pm when it is actually 08.15 am.
    Last edited by Philip M; 10-19-2006 at 09:19 AM. Reason: Typo

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    The following is one possible way of doing this (I did not verify that the logic is correct for all possible time differences) All hour values are 24 hr format: 0-23 -
    Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script>
    var serverhour = <?php echo date('H') ?> // server hour 0-23
    
    var d=new Date()
    var visitorhour=d.getHours() // visitor hour 0-23
    
    var adjust = serverhour - visitorhour // calculate the difference
    
    <?php
    $event_h = 10;
    $event_m = 30;
    ?>
    var etime = new Date()
    etime.setHours(<?php echo $event_h ?>,<?php echo $event_m ?>)  // set hour:min to 10:30
    
    var localetime = new Date()
    localetime.setHours(etime.getHours()+adjust,etime.getMinutes())  // add the difference to the hours
    
    </script>
    </head>
    
    <body>
    <script>document.write('The event at: '+etime.getHours()+':'+etime.getMinutes()+' server time, will occur at: '+ localetime.getHours()+':'+localetime.getMinutes()+' visitor\'s local time.')</script>
    </body>
    </html>
    This example uses PHP to generate the javascript. The serverhour variable is set to the current hour on the server. The visitorhour variable is set to the current hour from the visitor's browser and the difference between the two hour values is calculated. The etime variable is set to the event time at the server. The localtime variable is set to the etime variable with the hours adjusted by the difference between the server and local time.
    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
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Philip M View Post
    You can't as you have no way of knowing which time zone the user is in.

    I am in the UK and this forum's clock tells me the time is 11.15 pm when it is actually 08.15 am.
    Oh, but you can:
    Code:
    <script type="text/javascript">
    var today= new Date();
    alert(-today.getTimezoneOffset());//TimeZone in minutes
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    For Philip M:

    To get the times displayed in the Forum to be corrected for your time zone, go to the User CP, pick Edit Options, and change your time zone under the data and time options. The times shown on the posts and the last visited time will be corrected to when they occurred in your local time zone.
    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.


  •  

    Posting Permissions

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