What is the JavaScript equivalent to the following PHP code:
$expTime = time() + (5 * 60 * 60); // now plus 5 hours (5 hour; 60 mins; 60 secs)
$ex开发者_运维技巧pTimeStr = gmdate('Y-m-d\TH:i:s\Z', $expTime);
var expTime = new Date((+new Date()) + (5 * 60 * 60000))
var m = expTime.getMonth() + 1
var d = expTime.getDate()
var y = expTime.getFullYear()
var h = expTime.getHours()
var i = expTime.getMinutes()
var s = expTime.getSeconds()
var expTimeStr = y +"-"+ m +"-"+ d +" "+ h +":"+ i +":"+ s
The gmdate()
function doesn't transfer well to JavaScript... There is a gmdate()
port on phpjs that uses the date()
function from phpjs to do the complex formatting.
To calculate the date:
var time = new Date((+new Date()) + (5 * 60 * 60000)); // js times are ms
alert(time.toUTCString()); // quick JS method to return UTC time
The (+new Date())
forces the Date() into an integer before adding ms, and passing it back to the new Date()
constructor.
The built in date formatting for Javascript isn't incredible, but I can offer two helpful libraries to accomplish what you want. The first is something called phpjs - Javascript ports of a number of php functions.
http://phpjs.org/functions/index
http://phpjs.org/functions/gmdate:586
The second is a jQuery plugin:
http://joncom.be/code/jquery-phpdate/
精彩评论