Local Time Script

Discussion in 'HTML & Website Design' started by cdotech, Jun 19, 2007.

  1. #1
    Can someone point me to a script where it can show the local time in your country as I am running a LOCAL website (our area)??

    If there isn't any.. can someone pls recomment a good time and date script that can be easily customized.

    thanks! any help would be appreciated.
     
    cdotech, Jun 19, 2007 IP
  2. mudi

    mudi Peon

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PHP Code:
    echo date("l, F d, Y H:i:s" );

    Should do the trick It will show the local time on the Users's PC regardless of where they are.
     
    mudi, Jun 19, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    No... that is server time. To show user's local time, they need to set their timezone, or you could try guess it by a combination of IP->country lookups etc.

    Of course, you could use Javascript:
    d = new Date();
    d.toLocaleString(); // local time
    Code (markup):
     
    krt, Jun 19, 2007 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use a GMToffset variable, and the .getTimezoneOffset() method of the JavaScript date object. The code will always display "local" time, regardless of the user's time zone, and provided that the user's system clock is reasonably accurate. You will also have to change the offset variable,whenever the "local" time changes, due to DST.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <script type="text/javascript">
    
    	var GMToffset = -4;  //Your CURRENT local offset, whether Daylight or Standard time.
    
    	function dispClock(){
    
    		var PM = false;
    		var refDate = new Date();
    		refDate.setHours(GMToffset+refDate.getHours()+refDate.getTimezoneOffset()/60);
    		document.getElementById('localDay').innerHTML = refDate.toString().substring(0,3);
    		document.getElementById('localDate').innerHTML = refDate.toString().substring(4,7)+" "+refDate.getDate()+" "+refDate.getFullYear();
    		var nHours = refDate.getHours().toString();
    		var nMins = refDate.getMinutes().toString();
    		if (nMins < 10){nMins = "0"+nMins}
    		if (nHours > 12){nHours = nHours-12; PM=true}
    		var timeStr =  nHours+":"+nMins;
    		if (PM){timeStr += " PM"} 
    		if (!PM && nHours != 12){timeStr += " AM"}
    		if (!PM && nHours == 12){timeStr += " PM"}
    		document.getElementById('localTime').innerHTML = timeStr;
    		setTimeout("dispClock()",60000);
    	}
    
    	onload=dispClock;
    
    </script>
    </head>
    <body>
              <table border='1' align='center' cellspacing='0' cellpadding='5' style='font-size:14pt'>
                   <thead>
                        <tr>
                             <th>
                                  Location
                             </th>
                             <th>
                                  Day
                             </th>
                             <th>
                                  Date
                             </th>
                             <th>
                                  Time
                             </th>
                        </tr>
                   </thead>
                   <tr>
                        <td align='center'>
                             Boston, MA
                        </td>
                        <td id='localDay' align='center'></td>
                        <td id='localDate' align='center'></td>
                        <td id='localTime' align='center'></td>
                   </tr>
              </table>
         </body>
    </html>
    
    Code (markup):
     
    Mike H., Jun 20, 2007 IP
    cdotech likes this.
  5. cdotech

    cdotech Banned

    Messages:
    1,017
    Likes Received:
    154
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you Mike H. I am trying work on the script you gave. Rep Given!
     
    cdotech, Jun 20, 2007 IP