将人类日期转换为unix时间戳

问题描述:

I have a jQuery script that returns a date as:

Wed Nov 09 2011 16:30:00 GMT-0700 (MST)

How could I convert that into unix timestamp? I was looking at mktime() but I'm not really understanding it completely. Any ideas?

我有一个jQuery脚本,它返回一个日期: p>

Wed Nov 09 2011 16:30:00 GMT-0700(MST) p>

如何将其转换为unix时间戳? 我在看mktime(),但我并没有完全理解它。 有任何想法吗? p> div>

If you're using PHP 5.2, try the DateTime class, eg

$dt = new DateTime("Wed Nov 09 2011 16:30:00 GMT-0700 (MST)");
$ts = $dt->getTimestamp();

Otherwise, try strtotime(), eg

$ts = strtotime("Wed Nov 09 2011 16:30:00 GMT-0700 (MST)");
echo date("r", $ts);

For me, this outputs

Thu, 10 Nov 2011 10:30:00 +1100

Note that the date() function is local timezone aware

I take it that jQuery's using a Date object; instead, have the script send the value of Math.floor(theDate.getTime() / 1000) to your PHP script. That's the Unix timestamp you need.

What about strtotime ?

$test = strtotime('Wed Nov 09 2011 16:30:00 GMT-0700 (MST)');

echo $test;

output : 1320881400