创建php直播时钟

Ara*_*ani 1 php time clock

如何创建一个带有php的活动时钟从服务器获取时间而不是用户pc时间[不是javascript]

我使用下面的代码,但使用PHP变量时停止

<form name="Tick">
<input type="text" size="12" name="Clock">
</form>
<script type="text/javascript">
function show(){
    var hours="<?php echo $myhour; ?>"
    var minutes="<?php echo $mymin; ?>"
    var seconds="<?php echo $mysec; ?>"
    var dn="AM" 
    if (hours>12){
        dn="PM"
        hours=hours-12
        //this is so the hours written out is in 12-hour format, instead of the default //24-hour format.
    }
    if (hours==0)
        hours=12
    //this is so the hours written out when hours=0 (meaning 12a.m) is 12
    if (minutes<=9)
        minutes="0"+minutes
    if (seconds<=9)
        seconds="0"+seconds
    document.Tick.Clock.value=
    hours+":"+minutes+":"+seconds+" "+dn
    setTimeout("show()",1000)
}
    show()
</script>
Run Code Online (Sandbox Code Playgroud)

Tir*_*tel 10

你可以用ajax.

timestamp.php

<?php
    date_default_timezone_set('YOUR TIMEZONE');
    echo $timestamp = date('H:i:s');
Run Code Online (Sandbox Code Playgroud)

jQuery的

$(document).ready(function() {
    setInterval(timestamp, 1000);
});

function timestamp() {
    $.ajax({
        url: 'http://localhost/timestamp.php',
        success: function(data) {
            $('#timestamp').html(data);
        },
    });
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div id="timestamp"></div>
Run Code Online (Sandbox Code Playgroud)

  • @TirthPatel,它对我来说非常有效,我只想知道,这是最好的使用方式吗?因为在控制台中,ajax会在每个间隔调用一次 (2认同)