dallen
dallen

Reputation: 2651

Convert a human date to unix timestamp

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?

Upvotes: 0

Views: 4305

Answers (3)

Phil
Phil

Reputation: 164752

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

Upvotes: 3

malletjo
malletjo

Reputation: 1786

What about strtotime ?

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

echo $test;

output : 1320881400

Upvotes: 1

Ry-
Ry-

Reputation: 224886

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.

Upvotes: 1

Related Questions