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

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.