gpt4 book ai didi

php - 制作实时时钟javascript

转载 作者:可可西里 更新时间:2023-11-01 13:36:50 24 4
gpt4 key购买 nike

有谁知道如何让实时 javascript 时间运行..

我有这个 php 代码

    $expiredate = date('d m Y G:i:s', $rdate1);
$f_ex_date = explode(" ", $expiredate);
$f_ex_time = explode(":", $expiredate);
$_endDate = mktime($f_ex_date[0],$f_ex_date[1],$f_ex_date[2],$f_ex_date[1],$f_ex_date[0],$f_ex_date[2]);
$time = $_endDate - time();
$days = floor($time/86400);
$hours = floor(($time-($days*86400))/3600);
$mins = floor (($time-($days*86400)-($hours*3600))/60);
$secs = floor ($time-($days*86400)-($hours*3600)-($mins*60));

echo "Your account going to be expired in <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$days."</span> Days <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$hours."</span> Hours <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$mins."</span> Minutes <span style=\"color: #C11B17;font-family:arial;font-size: 16px;\">".$secs."</span> Seconds";

有没有可能让它像直播一样??

最佳答案

这是如何做到的。 Working Demo .

首先,在您的 HTML 文档的顶部:

.datetime {
color: #C11B17;
font-family:arial;
font-size: 16px;
}

我们这样做是为了稍微清理我们的 HTML 代码:

$rdate1 = 1240550032; // Fri, 24 Apr 2009 05:13:52 GMT
$expiredate = date('d m Y G:i:s', $rdate1);
$time = $rdate1 - time();
$days = floor($time/86400);
$hours = floor(($time-($days*86400))/3600);
$mins = floor(($time-($days*86400)-($hours*3600))/60);
$secs = floor($time-($days*86400)-($hours*3600)-($mins*60));

printf("
Your account is going to expire in
<span class='datetime' id='days'>%s</span> Days
<span class='datetime' id='hours'>%s</span> Hours
<span class='datetime' id='minutes'>%s</span> Minutes
<span class='datetime' id='seconds'>%s</span> Seconds
", $days, $hours, $mins, $secs);

我不太确定你采取的中间步骤是从哪里来的,但是上面的代码让我知道了 $rdate1 (大概是一个 unix 时间戳)和 之间的时间差时间()

最后,我们可以用 Javascript 做这样的事情来在页面加载后更新时间:

addEvent(window, 'load', function() {
var eDays = document.getElementById('days');
var eHours = document.getElementById('hours');
var eMinutes = document.getElementById('minutes');
var eSeconds = document.getElementById('seconds');
var timer;
timer = setInterval(function() {
var vDays = parseInt(eDays.innerHTML, 10);
var vHours = parseInt(eHours.innerHTML, 10);
var vMinutes = parseInt(eMinutes.innerHTML, 10);
var vSeconds = parseInt(eSeconds.innerHTML, 10);

vSeconds--;
if(vSeconds < 0) {
vSeconds = 59;
vMinutes--;
if(vMinutes < 0) {
vMinutes = 59;
vHours--;
if(vHours < 0) {
vHours = 23;
vDays--;
}
}
} else {
if(vSeconds == 0 &&
vMinutes == 0 &&
vHours == 0 &&
vDays == 0) {
clearInterval(timer);
}
}
eSeconds.innerHTML = vSeconds;
eMinutes.innerHTML = vMinutes;
eHours.innerHTML = vHours;
eDays.innerHTML = vDays;
}, 1000);
});


function addEvent( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
} else
obj.addEventListener( type, fn, false );
}

关于php - 制作实时时钟javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/763032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com