【问题标题】:How to display realtime change in server date and time every 1 Sec in php?如何在php中每1秒显示服务器日期和时间的实时变化?
【发布时间】:2017-05-22 13:04:24
【问题描述】:

我编写了这个 php 代码来显示服务器日期和时间,但我想每 1 秒显示一次服务器日期和时间的实时变化

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

请帮帮我,谢谢

【问题讨论】:

  • 你试过什么?你知道How To Ask吗?
  • 例如,我也试过这个 javascript 代码,它可以实时工作,但它显示的是我的电脑时间,而不是服务器日期和时间
  • 为什么不使用没有互联网流量的客户端时间。
  • 停止使用 PHP。

标签: php time clock


【解决方案1】:

您将需要使用 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>

【讨论】:

  • 此代码显示我的电脑日期和时间,而不是服务器日期和时间
  • 因为 JS 是前端编程语言,所以它需要从您的计算机中获取时间数据,如果您想要服务器时间,您需要将时间戳从 php 传递到 javascript,并且每秒添加一个并格式化以显示日期和时间
  • 我为你更新它以从服务器获取时间和日期
  • 如何将脚本更改为只有 H,m,s 格式和类似 对于 sze跨度>
【解决方案2】:

如果您仍然需要使用服务器时钟的实时时钟,您可以试试这个。我正在使用树枝{{now|date('Y/m/d H:i:s')}}。但你也可以使用 php 的&lt;?php echo date('Y/m/d H:i:s');?&gt;。它基本上使用localStorage 将服务器日期存储在localstoragesetSeconds 上每1 秒更新一次localstorage,而now 变量加载localstorage 日期并将其转换为js 日期格式。然后,我使用 date 元素内的 {{now|date('Y/m/d H:i:s')}} 进行后备,以防本地存储未启用。

  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);
}

【讨论】:

    【解决方案3】:

    您可以在页面加载时获取服务器时间,并使用 javascript 函数在本地每秒更新时间。 HTML 页面

    <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>
    

    clock.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;
            }
        }
    });
    

    script.php

    <?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

    【讨论】:

      【解决方案4】:

      使用 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());
      ?>
      

      下载文件here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-24
        • 1970-01-01
        • 2014-08-27
        • 2012-10-16
        • 2016-11-02
        • 2020-11-22
        • 2018-01-31
        • 1970-01-01
        相关资源
        最近更新 更多