php time refresh

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
potato_chip
Forum Newbie
Posts: 20
Joined: Mon Oct 06, 2008 10:38 am

php time refresh

Post 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!!!
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: php time refresh

Post 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.
potato_chip
Forum Newbie
Posts: 20
Joined: Mon Oct 06, 2008 10:38 am

Re: php time refresh

Post 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. ????
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: php time refresh

Post 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).
madan koshti
Forum Commoner
Posts: 50
Joined: Fri Jun 06, 2008 4:25 am

Re: php time refresh

Post by madan koshti »

<br>
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: php time refresh

Post 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.
potato_chip
Forum Newbie
Posts: 20
Joined: Mon Oct 06, 2008 10:38 am

Re: php time refresh

Post by potato_chip »

Thanks very much to you all! You are the best!
Post Reply