2

How do I convert this timestamp from php into a javascript Date() object?

This is how I grab the time:

$timestart = time();

and I parse this to a javascript function and I want to convert it into a JavaScript date object.

help, all this date stuff confuses me quite a bit.

thanks,

asked Apr 19, 2011 at 19:51
2
  • possible duplicate of Convert a Unix timestamp to time in Javascript Commented Apr 19, 2011 at 20:07
  • generic JS implementations are required to understand RFC-1123 datetime strings. Commented Apr 19, 2011 at 22:10

3 Answers 3

3

If val contains your PHP value which is

the current time measured in the number of seconds since the Unix Epoch

then you just need this:

var timestart = new Date(val * 1000);

JavaScript uses the same base time as UNIX systems (midnight on 01/01/1970) but measured in milliseconds rather than seconds.

answered Apr 19, 2011 at 19:55
Sign up to request clarification or add additional context in comments.

Comments

1
answered Apr 19, 2011 at 19:58

Comments

0

Substring the parts of the timestamp you need to create the Date. Then initialise like so,

var d = new Date(year, month, date);

This is a cross browser implementation.

answered Apr 19, 2011 at 19:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.