将PHP Unix时间戳转换为Javascript时间戳格式

将PHP Unix时间戳转换为Javascript时间戳格式

问题描述:

我正在将一个unix时间戳从我的php扔到我的javascript中,我注意到PHP和Javascript时间戳与纪元不同(秒与毫秒)。

I'm throwing a unix timestamp from my php to my javascript and I noticed that PHP and Javascript timestamps are different (seconds vs. milliseconds) from the epoch.

我基本上做的是回应php unixtime然后添加3个零(简单地将它乘以1000)但我注意到当我检查它时,它显示时间约为4-8小时。

What I'm basically doing is to echo the php unixtime then add 3 zeroes (as to simply multiply it by 1000) but I noticed that when I check it, it shows that the time is off by around 4-8 hours.

我使用的是canvas.js,我需要使用unix时间戳转换它

I am using canvas.js and I need to convert it using the unix timestamp

例如:


1434183780

1434183780

2015年6月13日上午8:23

Jun 13 2015 8:23AM

我添加3个零


1434183780000

1434183780000



echo "{ x:".$chartData[$loop]['time']."000 , y:1 }";

如果时间变为:

2015年6月13日16:23 PM

Jun 13 2015 16:23PM

一切正常,但时间变得完全失真。

Everything is working fine except that the time becomes completely distorted.

一旦我把它放在javascript上,我怎么能在没有时间改变的情况下使它工作?我宁愿把它保持在unixtime中,因为当我用它做其他事情的时候我仍然使用它。

How could I make it work without the time being changed once I put it on javascript? I'd prefer to keep it in unixtime as I still use it in that format when I do something else with it.

这听起来就像是时区问题。

This sounds just like a time zone issue.

Unix时间戳始终是 UTC 中的一个时间点。

A Unix timestamp is always a point in time in UTC.

当以2015年6月13日上午8:23的格式显示时,您总是使用某个时区来显示它。时间显示为16:23 PM的原因是因为正在使用除您期望之外的时区来显示Unix时间戳。

When displaying it in a format like "Jun 13 2015 8:23AM" you are always using a certain time zone to display it as such. The reason the time is being displayed as "16:23PM" is because a time zone other than you expect is being used to display the Unix timestamp.

所以解决方案是只需确保在显示时间戳时选择正确的时区。

So the solution is simply making sure you're picking the correct time zone when displaying the timestamp.

如果您使用的是JavaScript 日期对象,你的意思是使用UTC,你可以尝试使用像这样的方法toUTCString()

If you're using the JavaScript Date object and you mean to use UTC, you can try using a method like toUTCString():

console.log(new Date(1434183780000).toUTCString());
// Output: Sat, 13 Jun 2015 08:23:00 GMT

日期仅支持本地时区或UTC。如果要以UTC格式构建自己的格式化字符串,可以使用日期中的方法,以 getUTC *()开头>,如 getUTCHours() getUTCDate()

Date supports only the local time zone or UTC. If you want to construct your own formatted string in UTC, you can use the methods in Date starting with getUTC*(), like getUTCHours() or getUTCDate() .

在日期对象的更多信息/ Global_Objects / Daterel =nofollow> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

See more info on the Date object in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date