Jump to content

How can I make this clock ticking on my website??(without refreshing the page)


kokoko

Recommended Posts

*I googled this problem and couldn't find any..

 

This is the code. What I've changed are only datetimezone and date format.

 

<?php

$timezone = new DateTimeZone( "Asia/Seoul" );

$date = new DateTime();

$date->setTimezone( $timezone );

echo  $date->format( 'h:i:s A (l, F jS, Y)' );

?> 

 

I mush refresh the page to see the current time. Could anyone tell me how to make this clock ticking without refreshing the page?

I want to see every hour, minute, and second ticking. Please help~!! Thank you in advance :D

  • 2 weeks later...

As php is a serverside, you would need to force the page to refresh to acheive this.

 

A better way is to use either Javascript or ajax.

 

javascript is a good choice.

 


<script type="text/javascript">
<!--

function updateClock ( )
{
  var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Choose either "AM" or "PM" as appropriate
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

  // Convert an hours component of "0" to "12"
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;

  // Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

// -->
</script>

<body onload="updateClock(); setInterval('updateClock()', 1000 )">


<span id="clock"> </span>


 

 

i just modified the code. take note.

Thank you for the answers and the script!! But it needs to show the current local time in Seoul, Korea, not the clock on the visitor's desktop. Where and how do I do that? I found only one script but it was wayyyyyyyyy to long to put in.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.