【发布时间】:2017-11-05 01:02:42
【问题描述】:
enter image description here
基本上,我用 SQL 查询插入了一个带有日期时间 + 间隔(未来的东西)的行。
$interval = new DateInterval('PT'.$H.'H'.$i.'M'.$s.'S');
$date = new DateTime(); $date->add($interval);
$query = $conn->prepare("INSERT INTO profiles_in_missions (id_pim, id_profile, id_mission, time) VALUES (NULL, :idprofile, :idmission,:time)");
$query->bindValue(':idprofile', $tableau[0]);
$query->bindValue(':idmission', $id);
$query->bindValue(':time', $date->format('Y-m-d H:i:s'));
$query->execute();
如果我的电脑显示:23:40,并且如果我以 +8 分钟的间隔插入 DateTime,则此查询将 21:48 存储在数据库中。到目前为止,我的数据库是 GTM+00,我的电脑默认浏览器是 GTM+2。
存储后,我会尝试选择(在这种情况下)-2h+8m 的日期并进行倒计时。
现在的问题是: 为了进行倒计时,我正在使用 javascript 并且我执行 21:48-now();但他总是会比平时快 2 小时结束,因为在 MYSQL 中使用 GTM+00 BUT Javascript now() 存储的日期(21:48);正在获取我的默认浏览器时间 GTM+2。
有没有办法让 Javascript 与服务器 Timezone GTM+00 一起工作?我该如何解决我的问题?我所有的倒计时代码都有:
<script>
var t = document.getElementById('myInputTimer').value;
var countDownDate = new Date(t).getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
【问题讨论】:
标签: javascript php mysql datetime