How to dynamically display time using PHP?

I want to dynamically display time using PHP in a web browser.

My code is:

<?php
date_default_timezone_set('UTC');
for($j=0;$j<5;$j++)
{
echo nl2br("\n");
sleep(1);
echo date(" H:i:s", time());
}
?>

The web page displays a list of 5 time stamps together, instead of printing them every second. I do not know where I was wrong.

+4
source share
4 answers

PHP is server side code. After it has been sent to the client, it can no longer refresh the page. To do what you need, you need JavaScript.

Quick Google has put up a number of websites that can help you. This one looks pretty easy to implement.

+6
source

PHP , . PHP . , JavaScript.

JavaScript Date() , . - , Date(), .

- :

CodePen: http://codepen.io/anon/pen/IAKht

<?php
date_default_timezone_set('UTC');
?>
<html>
<head>
<script>
var d = new Date(<?php echo time() * 1000 ?>);

function updateClock() {
  // Increment the date
  d.setTime(d.getTime() + 1000);

  // Translate time to pieces
  var currentHours = d.getHours();
  var currentMinutes = d.getMinutes();
  var currentSeconds = d.getSeconds();

  // Add the beginning zero to minutes and seconds if needed
  currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
  currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

  // Determine the meridian
  var meridian = (currentHours < 12) ? "am" : "pm";

  // Convert the hours out of 24-hour time
  currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
  currentHours = (currentHours == 0) ? 12 : currentHours;

  // Generate the display string
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + meridian;

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

window.onload = function() {
  updateClock();
  setInterval('updateClock()', 1000);
}
</script>
</head>
<body>
  <div id="clock">&nbsp;</div>
</body>
</html>
0

php - . , , HTML. - . , .

To do this, you must display the time at regular intervals. There are two ways to make this client and server side, which is located below,

Server-side time retrieval method

<html>
<body>

    <div id="timeNow" >
    </div>

    <script>
        var d = new Date(<?php date_default_timezone_set('UTC'); echo strtotime('now')*1000 ?>);
        (function foo(){
            d.setTime(d.getTime()+1000);
            var clientTime = d.getHours() + ":"  + d.getMinutes() + ":" + d.getSeconds() + " " + (d.getHours() >= 12 ? 'pm' : 'am');
            //alert(clientTime);
            document.getElementById("timeNow").innerHTML = clientTime;
            setTimeout(foo, 1000); // refresh time every 1 second
        })();
    </script>

</body>
</html>

Get Time Client Method :

<html>
<body>

    <div id="timeNow" >
    </div>

    <script>
        (function foo(){
            var d = new Date();
            var hours = d.getHours()*12;
            var clientTime = (hours ? hours : 12) + ":"  + d.getMinutes() + ":" + d.getSeconds() + " " + (hours >= 12 ? 'pm' : 'am');
            //alert(clientTime);
            document.getElementById("timeNow").innerHTML = clientTime;
            setTimeout(foo, 1000); // refresh time every 1 second
        })();
    </script>

</body>
</html>
-1
source

I think php for javascript is not such a good idea ... you will not make a server call every second. Better is a javascript client solution.

-1
source

All Articles