在PHP中,我们可以使用date()函数来获取当前的时间,并使用echo语句将其打印出来,为了实现实时更新的时间,我们可以使用while循环和sleep()函数来控制时间间隔,下面是一个简单的示例:
<?php
while (true) {
    $current_time = date("Y-m-d H:i:s");
    echo $current_time . "<br>";
    sleep(1); // 暂停1秒
}
?>
这段代码会无限循环地打印当前时间,并在每次打印后暂停1秒,你可以根据需要调整sleep()函数中的参数来改变时间间隔。
这种方法并不是最佳实践,因为它会导致服务器资源浪费,更好的方法是使用JavaScript来实现实时更新的时间,下面是一个使用JavaScript实现实时更新时间的示例:
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>实时时间</title>
</head>
<body>
    <h1 id="time"></h1>
    <script>
        function updateTime() {
            var now = new Date();
            var timeString = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
            document.getElementById('time').innerHTML = timeString;
        }
        setInterval(updateTime, 1000); // 每隔1秒更新一次时间
    </script>
</body>
</html>
这段代码会在网页上显示一个实时更新的当前时间,并且每隔1秒更新一次,你可以根据需要调整setInterval()函数中的参数来改变时间间隔。




 
		 
		 
		
还没有评论,来说两句吧...