Page 1 of 1

php time refresh

Posted: Tue Oct 14, 2008 3:56 pm
by potato_chip
I was able to display the current time on my page, like this:

Code: Select all

<?php
//display today's date, time in top right of each page. 
$todaydate = date('F j, Y - h:i A');
echo "$todaydate&nbsp;&nbsp;";
?>
This code is in my topmain.php. And topmain.php is included in the index.php. Once the index.php file is loaded, the time displayed is the time when the page was loaded. Does anyone knows how to display the real time as the clock clicks.

Thanks!!!

Re: php time refresh

Posted: Tue Oct 14, 2008 5:00 pm
by Ziq
This is no php task. You should use javascript (client-side) language to do this. Or use their together. Use PHP to set current time and javascript to get a dynamic effect.

Re: php time refresh

Posted: Tue Oct 14, 2008 6:05 pm
by potato_chip
But how to use the server php time() and pass this to javascript?

I know how to do in javascript, and here is the code:

Code: Select all

// JavaScript Document
function updateClock ( )
{
  var currentTime = new Date();
 
    var currentMonth = currentTime.getMonth() + 1;
    var currentDay = currentTime.getDate();
    var currentYear = currentTime.getFullYear();
    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 = currentMonth + "/" + currentDay + "/" + currentYear + "  " + currentHours + ":" + currentMinutes + " " + timeOfDay;
 
  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
 
and here is the body part, to call the updateClock() function every second:

Code: Select all

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

The only thing I know so far is like this:

Code: Select all

var currentTime = <?php echo time(); ?>;
But the time format between php and javascript are totally different. ????

Re: php time refresh

Posted: Wed Oct 15, 2008 2:54 am
by Ziq
OK, I'm no pro in javascript, but I try to help you. Actually, we should create 2 javascript task. Firstly, to set current date, secondly, to display dynamic clock. I created this simple code:

Code: Select all

 
<script language="Javascript">
<!--
var hours = <? echo date('h'); ?>
var minutes = <? echo date('i'); ?>
var seconds = <? echo date('s'); ?>
 
function showtime () 
{
    seconds = seconds + 1
    if (seconds == 60)
    { 
        seconds = 0
        minutes = minutes + 1
    }
    if (minutes == 60)
    { 
        minutes = 0
        hours = hours + 1
    }
    if (hours == 24)
    { 
        hours = 0
    }
    timerID = setTimeout("showtime()",1000);
    currentTimeString = hours + ":" + minutes + ":" + seconds
    document.getElementById("clock").innerHTML = currentTimeString
}
//-->
 
</script>
<body onLoad="showtime()">
<p id="clock"></p>
</body>
 
This is really simple code and you should change it. This one show time in European style (without AM and PM, it's 24 hours).

Re: php time refresh

Posted: Wed Oct 15, 2008 9:27 am
by madan koshti
<br>

Re: php time refresh

Posted: Wed Oct 15, 2008 9:50 am
by omniuni
If you wanted a javascriptless solution, you could make a small iFrame with a metadata-refresh. It would cause the time to update every x number of seconds.

Re: php time refresh

Posted: Wed Oct 15, 2008 11:04 am
by potato_chip
Thanks very much to you all! You are the best!