简体   繁体   中英

How can I convert a php timestamp to the same format as new Date() in javaScript?

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?

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

看一下日期功能...

echo date("DM j G:i:s TY");

/* 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()

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM