我有一种情况,每当我查看时间时,我需要每 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
}
我知道这篇文章可能有点困惑,但我希望它是可以理解的。
有什么想法吗?
谢谢
最佳答案
使用TIME_TO_SEC(TIMEDIFF(final_time, initial_time))
SELECT TIME_TO_SEC(TIMEDIFF('17:00:00', '09:00:00')) -- 28800
SELECT TIME_TO_SEC(TIMEDIFF('12:30:00', '12:00:00')) -- 1800
SELECT TIME_TO_SEC(TIMEDIFF('10:30:00', '10:15:00')) -- 900
关于php - 如何用php和mysql计算剩余时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338824/