oneofakind
oneofakind

Reputation: 562

Javascript time to PHP time

I have a time value in javascript for example 11:30:00.

 Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}

and I passed to a php file and converted it by:

 $newTime = date('H:i:s', $theTime);

But, it return 05:36:00. What is the right way of concerting time?

Upvotes: 5

Views: 8758

Answers (2)

nickhar
nickhar

Reputation: 20913

If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:

$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

Which would output:

2012-10-22 04:30:00

This output is GMT, which you can change if required.

EDIT:

If you're expecting 11:30:00, then try the following:

date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

Upvotes: 4

Sean Kinsey
Sean Kinsey

Reputation: 38046

Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.

Upvotes: 13

Related Questions