在将数据从PHP传输到JavaScript时计算时区偏移量

在将数据从PHP传输到JavaScript时计算时区偏移量,php,javascript,timezone,Php,Javascript,Timezone,好吧,这是个奇怪的问题。在我的一个修改中,PHP代码使用time()将时间戳存储在数据库中-据我所知,这使用UTC时区。现在,我获取这些数据并将其用作某个Javascript函数中的参数。此函数返回类似于“30秒前”或“2小时30分钟前”等的时间字符串。以下是完整的函数: time_string : function(seconds,complexity,seconds_left,suffix,prefix){ var difference = Math.abs(dynamo.time

好吧,这是个奇怪的问题。在我的一个修改中,PHP代码使用time()将时间戳存储在数据库中-据我所知,这使用UTC时区。现在,我获取这些数据并将其用作某个Javascript函数中的参数。此函数返回类似于“30秒前”或“2小时30分钟前”等的时间字符串。以下是完整的函数:

time_string : function(seconds,complexity,seconds_left,suffix,prefix){
    var difference = Math.abs(dynamo.time - seconds);
    seconds_left = seconds_left == null ? 0 : seconds_left;
    seconds = +seconds;
    if(difference < 15 && !seconds_left){
        return "Just now";
    }
    suffix = suffix != null
        ? (seconds_left == 1
            ? ""
            : suffix)
        : (seconds_left == 1
            ? ""
            : " ago");
    prefix = prefix != null ? prefix : "";
    if(difference > 86400){
        return new Date(seconds * 1000).toDateString();
    } else {
        var years = Math.floor(difference / 31536000);
        var days = Math.floor((difference / 86400) % 365);
        var hours = Math.floor((difference / 3600) % 24);
        var minutes = Math.floor((difference / 60) % 60);
        var second = difference % 60;
        var time_str = [];
        if(years > 0) time_str[time_str.length] = dynamo.toolbox.format_number(years) + " year" + (years == 1 ? '' : 's');
        if(days > 0) time_str[time_str.length] = days + " day" + (days == 1 ? '' : 's');
        if(hours > 0) time_str[time_str.length] = hours + " hour" + (hours == 1 ? '' : 's');
        if(days <= 1){
            if(minutes > 0) time_str[time_str.length] = minutes + " minute" + (minutes == 1 ? '' : 's');
            if(second > 0 && !minutes && !hours && !days){
                time_str[time_str.length] = second + " second" + (second == 1 ? '' : 's');
            }
        }
        complexity = complexity != null ? complexity : 2;
        time_str = time_str.slice(0,complexity);
        if(time_str.length > 1){
            var final_str = time_str[time_str.length-1];
            time_str.pop();
            time_str = time_str.join(", ") + " and " + final_str;
        } else {
            time_str = time_str.join("");
        }
    }
    return prefix + time_str + suffix;
}
time\u字符串:函数(秒、复杂度、左秒、后缀、前缀){
var差=数学绝对值(发电机时间-秒);
剩余秒数=剩余秒数==null?0:剩余秒数;
秒=+秒;
如果(差值<15&!秒){
返回“刚才”;
}
后缀=后缀!=空
?(剩余秒=1
? ""
:后缀)
:(剩余秒==1
? ""
:“前”);
前缀=前缀!=空?前缀:“”;
如果(差异>86400){
返回新日期(秒*1000).toDateString();
}否则{
变量年份=数学下限(差值/31536000);
变量天数=数学下限((差/86400)%365);
变量小时数=数学下限((差值/3600)%24);
变量分钟数=数学下限((差值/60)%60);
第二个变量=差异%60;
var time_str=[];
如果(年>0)时间长度[时间长度]=dynamo.toolbox.format(年)+“年”+(年==1?'''s');
如果(天>0)时间长度[时间长度]=days+“day”+(days==1?“'s”);
如果(小时>0)时间长度[时间长度]=hours+“hour”+(hours==1?“'s”);
如果(第0天)时间长度[时间长度]=分钟+“分钟”+(分钟==1?“'s”);
如果(秒>0&&!分钟&&!小时&&!天){
时间长度=秒+“秒”+(秒==1?'''s');
}
}
复杂性=复杂性!=null?复杂性:2;
时间长度=时间长度切片(0,复杂度);
如果(时间长度>1){
var final_str=时间长度[时间长度-1];
time_str.pop();
time_str=time_str.join(“,”+”和“+final_str;
}否则{
time\u str=time\u str.join(“”);
}
}
返回前缀+时间+后缀;
}
在代码中,使用了发电机时间
dynamo.time
从PHP设置为当前
time()
(我使用它计算存储时间和当前时间之间的时间差)

但是,我需要调整
差异
变量,以说明客户端的时区差异与服务器的时区差异(与以前一样,我认为是UTC)

我该怎么做呢

如果您需要,这里有一个JS小提琴,对函数进行一些测试:

time_string : function(seconds,complexity,seconds_left,suffix,prefix){
    var difference = Math.abs(dynamo.time - seconds);
    seconds_left = seconds_left == null ? 0 : seconds_left;
    seconds = +seconds;
    if(difference < 15 && !seconds_left){
        return "Just now";
    }
    suffix = suffix != null
        ? (seconds_left == 1
            ? ""
            : suffix)
        : (seconds_left == 1
            ? ""
            : " ago");
    prefix = prefix != null ? prefix : "";
    if(difference > 86400){
        return new Date(seconds * 1000).toDateString();
    } else {
        var years = Math.floor(difference / 31536000);
        var days = Math.floor((difference / 86400) % 365);
        var hours = Math.floor((difference / 3600) % 24);
        var minutes = Math.floor((difference / 60) % 60);
        var second = difference % 60;
        var time_str = [];
        if(years > 0) time_str[time_str.length] = dynamo.toolbox.format_number(years) + " year" + (years == 1 ? '' : 's');
        if(days > 0) time_str[time_str.length] = days + " day" + (days == 1 ? '' : 's');
        if(hours > 0) time_str[time_str.length] = hours + " hour" + (hours == 1 ? '' : 's');
        if(days <= 1){
            if(minutes > 0) time_str[time_str.length] = minutes + " minute" + (minutes == 1 ? '' : 's');
            if(second > 0 && !minutes && !hours && !days){
                time_str[time_str.length] = second + " second" + (second == 1 ? '' : 's');
            }
        }
        complexity = complexity != null ? complexity : 2;
        time_str = time_str.slice(0,complexity);
        if(time_str.length > 1){
            var final_str = time_str[time_str.length-1];
            time_str.pop();
            time_str = time_str.join(", ") + " and " + final_str;
        } else {
            time_str = time_str.join("");
        }
    }
    return prefix + time_str + suffix;
}

编辑:我是否可以使用new Date().getTimezoneOffset()来实现这一点?唯一的问题是,DST将出现不一致。

我不确定这种混乱是从哪里来的;假设访客系统时钟足够好,您可以将任何时间戳与此进行比较:

Math.round(new Date().getTime() / 1000)
它还返回相同类型的unix时间戳,因此没有时区偏移的复杂性

用户系统时钟可能太乱了。在这些情况下,您可以进行时间偏移计算,通过内联JavaScript传递PHP的
time()
。同样,没有时区:)