簡體   English   中英

如何在php中每1秒顯示服務器日期和時間的實時變化?

[英]How to display realtime change in server date and time every 1 Sec in php?

我寫了這個 php 代碼來顯示服務器日期和時間,但我想每 1 秒顯示一次服務器日期和時間的實時變化

<p><?php echo "Server Time " . date("Y-m-d h:i:s"); ?> (GMT) UTC +0 UK/London</p>

請幫助我,謝謝

您將需要使用 Javascript,如下所示:

<body>
<p id="time"></p>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
var timestamp = '<?=time();?>';
function updateTime(){
  $('#time').html(Date(timestamp));
  timestamp++;
}
$(function(){
  setInterval(updateTime, 1000);
});
</script>

如果您仍然需要一個使用您的服務器時鍾的實時時鍾,您可以試試這個。 我正在使用樹枝{{now|date('Y/m/d H:i:s')}} 但你也可以使用 php 的<?php echo date('Y/m/d H:i:s');?> 它基本上使用localStorage將服務器日期存儲在localstoragesetSeconds每 1 秒更新一次localstorage ,而 now 變量加載localstorage日期並將其轉換為 js 日期格式。 然后,我在date元素中使用{{now|date('Y/m/d H:i:s')}}進行回退,以防未啟用 localstorage。

  try {
localStorage.setItem('today', new Date("{{now|date('Y/m/d H:i:s')}}");
setInterval(function clock() {
    var month = [
    "Jan", "Feb", "Marh", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Octr", "Nov", "Dec"
    ];
    var now = new Date(localStorage.getItem('today'));
    now.setSeconds(now.getSeconds() + 1);
    localStorage.setItem('today', now);
    var G = format(now.getHours() % 12 || 12);
    var i = format(now.getMinutes());
    var s = format(now.getSeconds());
    var M = month[now.getMonth()];
    var d = format(now.getDate());
    var Y = now.getFullYear();
    function format(data) {
        return (data < 10 ? data = "0" + data : data);
    }
    $("#date").html(M + ". " + d + ", " + Y + " " + G + ":" + i + ":" + s);
    return clock;
}(), 1000);
} catch(e) {
    console.log(e);
}

您可以在頁面加載時獲取服務器時間,並使用 javascript 函數在本地每秒更新時間。 網頁

<script> var JS_BASE_URL = 'http://YOURSERVER/';</script>
<script src="assets/js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script src="clock.js" type="text/javascript"></script>
</head>
<body>
Server Time  <span id="server_time">00:00:00</span>
</body>

時鍾.js

var url = JS_BASE_URL+'/script.php';
var _h = 0;
var _m = 0;
var _s = 0;
$.ajax({ 
    url: url,
    type: 'GET',
    dataType: 'JSON', 
    success: function(res) {
        var timer = setInterval(serverTime,1000);
        function serverTime(){
            h = parseInt(res.hour)+_h;
            m = parseInt(res.minute)+_m;
            s = parseInt(res.second)+_s;
            if (s>59){                  
                s=s-60;
                _s=_s-60;                   
            }
            if(s==59){
                _m++;   
            }
            if (m>59){
                m=m-60;
                _m=_m-60;                   
            }
            if(m==59&&s==59){
                _h++;   
            }   
            _s++;
            $('#server_time').html(append_zero(h)+':'+append_zero(m)+':'+append_zero(s));               }
        function append_zero(n){
            if(n<10){
                return '0'+n;
            }
            else
                return n;
        }
    }
});

腳本文件

<?php
$data = array('fulldate'=>date('d-m-Y H:i:s'),
              'date'=>date('d'),
              'month'=>date('m'),
              'year'=>date('Y'),
              'hour'=>date('H'),
              'minute'=>date('i'),
              'second'=>date('s')
        );
        echo json_encode($data);
?>

檢查github: https : //github.com/mdanielk/server_time

使用 Ajax 顯示顯示服務器時間的更改時鍾。 為此創建兩個文件,分別是用 Ajax 發送請求和接收數據的文件。

服務器時鍾.php

開發一個腳本,通過單擊按鈕,我們可以向服務器發送請求以獲取數據。 在這個文件的正文中,我們有一個按鈕。

 <input type=button value=
'Get Server Time' onclick="timer_function();">  

在這個腳本中,我們將添加一個計時器,以每秒遞歸調用相同的 Ajax 函數。 這將每秒獲取數據,因此我們可以顯示顯示服務器時間的變化時鍾。

function timer_function(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('AjaxFunction();',refresh)
}

定時器函數:timer_function()

單擊此按鈕時,它會觸發一個使用計時器 setTimeout 的函數。 在這個定時器函數中,它可以改變以毫秒為單位的刷新率。 在這個函數中,我們調用我們的主要 Ajax 函數 AjaxFunction()

function timer_function(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('AjaxFunction();',refresh)
}

在 AjaxFunction() 結束時再次調用 timer_function() 使其遞歸。

tt=timer_function();

在主 AjaxFunction() 中發送請求到 clock.php 文件並獲取服務器時間。 此數據使用 div 層顯示。

if(httpxml.readyState==4)
      {
document.getElementById("msg").innerHTML=httpxml.responseText;
document.getElementById("msg").style.background='#f1f1f1';
      }

第二個文件是簡單的 PHP 文件,其中一行代碼給出了服務器的當前日期和時間。 時鍾.php

<?Php
echo date("d/m/y : H:i:s", time());
?>

這里下載文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM