Monday, August 12, 2013

How to display clock using Javascript in PHP?

The following snippet of code display current time in your web page.

JavaScript
 <script  type="text/javascript" >
   function updateClock () {
    var currentTime = new Date();
    var month = currentTime.getMonth();
    var date = currentTime.getDate();
    var year = currentTime.getFullYear();
    var currentHours = currentTime.getHours ();
    var currentMinutes = currentTime.getMinutes ();
    var currentSeconds = currentTime.getSeconds ();
   
    var months = new Array(
    "January", "February", "March", "April",
    "May", "June", "July", "August", "September",
    "October", "November", "December");
   
    // Pad the minutes and seconds with leading zeros, if required
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
   
    var currentTimeString = months[month]+" "+ date +", "+year+" "+currentHours + ":" + currentMinutes + ":" + currentSeconds;
   
    document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
</script>

HTML Code
<body onload="updateClock(); setInterval('updateClock()', 1000 )">
<div id="clock" align="center" style="color:#F30; font-weight:100;">&nbsp;</div>
</body>


Have a nice day....

No comments:

Post a Comment