在计算一个PHP的时间差/ MySQL的/ JavaScript的系统 [英] Calculating the time difference in an PHP/MySQL/JavaScript system

查看:145
本文介绍了在计算一个PHP的时间差/ MySQL的/ JavaScript的系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,最好的办法是计算时间差,从现在到某一点时,我们说的倒计时时间是什么。

I was wondering what the best way is to calculate the difference in time from now to a certain point, let's say the countdown time.

我有,有一个closetime在某一点上拍卖,这一次是存储在MySQL记录格式为DATETIME 00-00-000 00:00:00。这个记录被称为closetime。

I have an auction that has a closetime at a certain point, this time is stored in a MySQL record in the format " DATETIME 00-00-000 00:00:00 ". This record is called closetime.

现在在我的网站我的JavaScript code,它通过一个PHP文件获取这个时候。 JavaScript的循环每秒钟使用的setInterval 1000 。 PHP文件会从DB中closetime,并将其发送回此格式

Now on my website I have JavaScript code that gets this time via a PHP file. The JavaScript loops every second using setInterval 1000. The PHP file gets the closetime from the db, and sends it back in this format

strtotime($result['closetime']);

和我得到的请求的时候,我想使用服务器的时间,而不是在JavaScript中的时间,因为用户的时钟可以关闭。

And I get the time of the request, I want to use the server time, and not the time in JavaScript, because the clock of the user can be off.

strtotime(date("H:i:s", $_SERVER['REQUEST_TIME']))

我发送回这两个时间戳,并计算在JavaScript它们之间的时间差。我用这个功能来做到这一点,值发送从PHP回我打电话currentTime的输出且CloseTime,我想这应该是明确的。

I send back these two timestamps and calculate the time difference between them in JavaScript. I use this function to do it, the values send back from PHP I call currentTime and closeTime, I think this should be clear.

function auctionDelayTime(currentTime,closeTime){
    totaldelay = closeTime - currentTime;
    if(totaldelay <= 0){
        return 'ended';
    }else{
        if( days=parseInt((Math.floor(totaldelay/86400))) )
            totaldelay = totaldelay % 86400;
        if( hours=parseInt((Math.floor(totaldelay/3600))) )
            totaldelay = totaldelay % 3600;
      if( minutes=parseInt((Math.floor(totaldelay/60))) )
            totaldelay = totaldelay % 60;
        if( seconds=parseInt((Math.floor(totaldelay/1))) )
            totaldelay = totaldelay % 1;

        return hours+':'+formatTimes(minutes)+':'+formatTimes(seconds);
    }
}
function formatTimes(value){
    return value < 10 ? '0'+value : value;
}

我觉得这是一个可怕的很多code做这么简单的东西。任何人都不会有一个更好的解决方案也许更美丽code。

I think this is an awful lot of code do something so simple. Does anyone have a better solution or maybe more 'beautiful' code.

享受!

推荐答案

使用 Date对象

var d = new Date(difference_in_milliseconds);
var seconds = d.getSeconds();
var minutes = d.getMinutes();
var hours = d.getHours() - 1; //See note

注:奇怪的是,时间价值就越大一个比我预期的原因,我不明白。它看起来像午夜1970年1月1日是凌晨1点: - )

Note: Strangely, hours value is bigger by one than I would expect for reason I don't understand. It looks like "midnight Jan 1, 1970" was at 1 AM :-)

更新:1的差异是由于我的时区(GMT + 8)的偏移

UPDATE: The difference of 1 is due to the offset of my timezone (GMT +1).

细微的变化,这将解决这个问题:

Slight change that will solve this:

var d = new Date();
var offset = d.getTimezoneOffset() * 60000;
var d = new Date(difference_in_milliseconds + offset);
var seconds = d.getSeconds();
var minutes = d.getMinutes();
var hours = d.getHours();

这篇关于在计算一个PHP的时间差/ MySQL的/ JavaScript的系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆