【发布时间】:2012-02-18 05:21:18
【问题描述】:
我有一种情况,每当我查看时间时,我需要每 6 小时计算一次剩余时间。
我有这个设置:
<div id="time"><div>
<button>trigger</button>
更准确地说,我有一个触发时间启动器:
$(buttom).on('click', function(){
..... send through ajax the current time to a php file
...if success, get the response and place it in the div
});
在 php 文件中我将该时间存储到数据库中
if (isset($_POST['time'])){
$storeTime->timestore($_POST['time']);
}
现在发生的事情是,每当我查看 div 时,我应该看到剩下的时间:
<div id="time">5h:50min<div>
我会在 30 分钟后再次访问
<div id="time">5h:20min<div>
等等。
问题不是使用ajax之类的来回发送时间,而是发送正确的时间。
我的想法是每次访问该页面时发送时间。第一次将其存储在表字段中,其他时间将它们存储在单独的表字段中
id time1 time2
1 123456.. 123124..
time1 保持不变,因为它是原始时间,每次我访问该页面时,我都会发送新的当前时间并更新 time2
这里我有点迷路了。
这就是我得到time1:$getTime = $data->getTime($userId);的方式
这是每次进来的时间:$time
我也知道6h是21600秒
所以
if ( $time >= ($newTime + 21600) ){
//if the current time is bigger than the first time + 6h it means that 6h have passed
// store the new time in the database for reference as the new main time
} else {
// 6h have not passed yet and we need to calculate how much time is left
//here i get confuzed
}
我知道这篇文章可能有点混乱,但我希望它可以理解。
有什么想法吗?
谢谢
【问题讨论】:
-
澄清一下,你问的是如何计算剩余时间?
-
是的,请,我不知道为什么我把它弄得这么复杂:)
-
下面告诉我你想要什么...
-
我认为 php 支持日期/时间的减法,所以你需要做的就是通过减去
time1和time2来获得经过的时间,然后从 6h 中的秒数中减去这个值(21600 )。这应该会给您剩余的秒数,您可以根据需要对其进行解析。 -
对你想要做什么有点困惑,但你为什么不在页面加载时通过剩余时间(或结束时间)然后用 javascript 更新它?
标签: php jquery mysql ajax time