2

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.

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.

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

For example:

1434183780

Jun 13 2015 8:23AM

I add 3 zeroes

1434183780000

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

and what happens is the time becomes:

Jun 13 2015 16:23PM

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

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.

3
  • The timestamp is UTC, perhaps local time is getting in the way? Commented Jun 23, 2015 at 2:58
  • are your referring to the PHP side or the Javascript side? I checked the PHP side and it is getting the correct data. Commented Jun 23, 2015 at 3:00
  • I'm talking about the client side. Commented Jun 23, 2015 at 6:09

1 Answer 1

4

This sounds just like a time zone issue.

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

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.

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

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() .

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

Sign up to request clarification or add additional context in comments.

3 Comments

Also of note is that a JS time is in micro seconds, so its a unix timestamp times 1000
It's more correct to say that the timestamp for a Date object is in milliseconds. JavaScript the language doesn't care about that. As far as I know, Date doesn't use anything more precise than milliseconds. I'm aware that microsecond precision is used as a float point number in the method Performance.now(), which is part of the High Resolution Time spec (see developer.mozilla.org/en-US/docs/Web/API/Performance).
Yes, of course milliseconds... I always get the two backwards! >.< Thanks for correcting me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.