【问题标题】:how to calculate the remaining left time with php and mysql?如何用php和mysql计算剩余时间?
【发布时间】: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-&gt;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 支持日期/时间的减法,所以你需要做的就是通过减去 time1time2 来获得经过的时间,然后从 6h 中的秒数中减去这个值(21600 )。这应该会给您剩余的秒数,您可以根据需要对其进行解析。
  • 对你想要做什么有点困惑,但你为什么不在页面加载时通过剩余时间(或结束时间)然后用 javascript 更新它?

标签: php jquery mysql ajax time


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果您要存储上次访问该网站的时间,您只需要存储一次。

    所以对于你的例子:

    $current_time          = time();
    $last_visit_time       = $data->getTime($userId);
    $since_last_visit_time = $current_time - $last_visit_time;
    $six_hours_in_seconds  = 21600;
    
    if($since_last_visit_time > $six_hours_in_seconds) {
        // not sure of the function call here so using yours
        // store new time as it's been over 6 hours
        $storeTime->timestore($current_time);
        $remaining_time = $six_hours_in_seconds;
    } else {
        $remaining_time = $six_hours_in_seconds - $since_last_visit_time;
    }
    
    echo "Remaining Seconds: {$remaining_time}<br />";
    

    第 2 部分使用 JavaScript / Ajax 你可以用它来显示剩余时间

    演示:

    JS

    var time_in_seconds = 21600; // this would be the $remaining_time PHP variable
    
    setInterval(function() {
        $('#countdown').html(seconds2time(time_in_seconds));
        time_in_seconds--;
    }, 1000);
    
    function seconds2time(seconds) {
        var hours   = Math.floor(seconds / 3600);
        var minutes = Math.floor((seconds - (hours * 3600)) / 60);
        var seconds = seconds - (hours * 3600) - (minutes * 60);
        var time = "";
    
        if (hours != 0) {
          time = hours+":";
        }
        if (minutes != 0 || time !== "") {
          minutes = (minutes < 10 && time !== "") ? "0"+minutes : String(minutes);
          time += minutes+":";
        }
        if (time === "") {
          time = seconds+"s";
        }
        else {
          time += (seconds < 10) ? "0"+seconds : String(seconds);
        }
        return time;
    }
    

    HTML

    <span id="countdown"></span>​
    

    【讨论】:

    • 可能就是这样,我想我想多了,让我试试,如果效果不好,请告诉你
    • 嗯,时间好像没变。我在数据库中插入时间,我可以看到剩下的时间是 5 小时:59 分钟,但它仍然存在。
    • 你想要一个恒定的倒计时?
    • 不,但我每次以$time = $_POST['time']查看该页面时都有时间进入
    • 我做了`$last_visit_time = $_POST['time']` 现在似乎从 0 开始 + 6 小时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多