5

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?

1
  • try passing the date in UTC. ie: (new Date).toJSON() Commented Oct 23, 2012 at 1:56

2 Answers 2

13

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

Sign up to request clarification or add additional context in comments.

1 Comment

i guess it worked. I tried this before but without dividing by 1000 that's wht it did not work. Thanks.
4

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));

6 Comments

I tried that but it returned 1970 01:00:00 but let me try that again
Works for me - tested it for sanity!
but at first it is on 11:30 but after converting it it turns to 4:30. Is that suppose to be 11:30 not 4:30?
You can set your default timezone in PHP: date_default_timezone_set('Asia/Taipei'); which would localise it. Just set that before you echo out the date/time.
Hang on - what are you expecting? 11:30?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.