views:

129

answers:

4

I have a time stamp I created with PHP that I would like to convert to the same format as new Date() in javaScript.

Here's my code:

$current_timestamp = time();
//This returns a value such as 1268214657 

I need to create a string using that timestamp that looks like this: Fri Mar 12 2010 01:50:33 GMT-0800 (Pacific Standard Time).

How can I create a string in php that has the same format as a new date would look like using new Date() in javaScript?

+2  A: 

The Date class has a constructor to pass in the number of milliseconds so multiply the PHP time in seconds by 1000:

var date = new Date(seconds * 1000);
alert(date);

Output:

Wed Mar 10 2010 17:50:57 GMT+0800

One issue you will face is the server generates time in one time zone, the client is in another. If you want to generate the timestamps on the server use the date() function:

echo date('r'); // assumes time(), r is RFC0822 format
cletus
The first suggestion you made was exactly what I needed. Thank you!
zeckdude
@cletus: thanks it's exactly what I was looking for.
Marco Demajo
+1  A: 

Take a look at date function...

echo date("D M j G:i:s T Y");

fire
+1  A: 
/* use the constants in the format parameter */
// prints something like: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);

php date function()

Sinan
+1  A: 

I would personally leave the PHP date as a Unit timestamp and change the Javascript.

You can read in a Unix timestamp quite easily:

var myDate = new Date();
myDate.setTime(unixtimestamp*1000);
alert(myDate.toUTCString);
Oli