如何计算剩余的剩余时间与PHP和MySQL? [英] how to calculate the remaining left time with php and mysql?
问题描述
我有一个情况我需要计算的剩余时间,每6个小时,每当我查看的时间。
i have a situation where i need to calculate the remaining time every 6 hours, whenever i view the time.
我有这样的设置:
<div id="time"><div>
<button>trigger</button>
更precise我有一个触发器,得到一次首发:
to be more precise i have a trigger that gets the time starter:
$(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文件我那个时候存储到数据库
in the php file i store that time to the database
if (isset($_POST['time'])){
$storeTime->timestore($_POST['time']);
}
会发生什么,现在是,每当我查看 DIV
我应该看到剩余时间:
what happens now is that whenever i view that div
i should see the left time:
<div id="time">5h:50min<div>
我再次vizit在30分钟内,我看到
i vizit again in 30 min i see
<div id="time">5h:20min<div>
等等。
这个问题不发送的时间来回用ajax或东西,但发送正确的时间。
the problem is not sending the time back and forth using ajax or something, but sending the correct time.
我在想什么是我每次访问的页面发送的时间。第一次将其存储在一个表中的字段和其他时间将它们存储在一个单独的表字段
What i was thinking is to send the time every time i visit the page. First time store it in a table field and the other times to store them in a separate table field
id time1 time2
1 123456.. 123124..
在时间1
保持未修改,因为它是原来的时间和每次访问我送的新的当前时间和更新页面时间2
the time1
stays unmodified as it is the original time and every time i visit the page i send the new current time and update time2
在这里,我得到一个有点失落。
here i get a bit lost.
这是我如何让时间1
: $的getTime = $数据 - &GT;的getTime($用户id);
和这是进来每次时间: $时间
and this is the time that comes in every time: $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
}
我知道这个帖子是有点混淆,也许,但我希望它可以理解的。
I know this post is a bit confuse maybe, but i hope its understandable.
什么想法?
感谢
推荐答案
如果你要保存的时间,当你上次访问,你只需要存储一次该网站。
If you're going to store the time for when you last visited the site you only need to store one time.
因此,对于你的例子:
So for your example:
$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 />";
使用JavaScript / AJAX,你可以用它来显示remaning时间第2部分
Part 2 using JavaScript / Ajax you can use this to display the remaning time
演示:
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
HTML
<span id="countdown"></span>
这篇关于如何计算剩余的剩余时间与PHP和MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!