Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 530
oneofakind Avatar asked Oct 23 '12 01:10

oneofakind


2 Answers

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

like image 148
Sean Kinsey Avatar answered Oct 21 '22 23:10

Sean Kinsey


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));
like image 25
nickhar Avatar answered Oct 21 '22 23:10

nickhar