1

I am getting a date format like Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time) form a javascript to my php function.

I want to store that to my database table in "2013-04-22 12:16:00" format.

Can any one help me to convert this date type.

I tried:

date("Y-m-d H:i:s",$startDate);

But it's giving error as

date() expects parameter 2 to be long string given

1
  • I am using mysql database.And the datatype of that field is datetime Commented Apr 27, 2013 at 19:54

3 Answers 3

5

Use strtotime() and date():

$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));

(see strtotime and date docs on the PHP site).

or use DateTime:

<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>

DEMO

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

8 Comments

Sorry it's giving me result "1970-01-01 01:00:00" But I am passing value in $startDate is "Mon Apr 22 2013 13:00:00 GMT+0530 (India Standard Time)"
I show the demo but sorry to say it's not working in my end..Now it's giving error like <b>Fatal error</b>: Uncaught exception 'Exception' with message 'DateTime::__construct() [&lt;a href='datetime.--construct'&gt;datetime.--construct&lt;/a&gt;]: Failed to parse time string (Mon Apr 22 2013 12:00:00 GMT+0530 (India Standard Time)) at position 41 (S): Double timezone specification'
Do u trying it in localhost or in the web ?
@amitghosh - just use string manipulation to remove the (India Standard Time) part of the stamp... you've already specified the timezone (GMT+0530). Try $startDate = preg_replace("/\((.*?)\)/", "", $startDate) and then run the $date = new DateTime($source); echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
|
2

Try using strtotime to convert the timestamp to unix-format so you can use it in the date() function:

date("Y-m-d H:i:s",strtotime($startDate));

You can also try using the (usually included) DateTime class. In particular, have a look at DateTime::createFromFormat. It may help you get around ambiguities in the date string (strtotime() will sometimes fail or mis-parse a date string). DateTime::createFromFormat allows you to specifically designate the format of the date string so there can be no ambiguity.

1 Comment

Sorry it's giving me result "1970-01-01 01:00:00" But I am passing value in $startDate is "Mon Apr 22 2013 13:00:00 GMT+0530 (India Standard Time)"
0

firstly you need to save it as Timestamp so,

$startDate = strtotime($javascript_value);    
$result =  date("Y-m-d H:i:s",$startDate);

1 Comment

it's also giving me "1970-01-01 01:00:00" I think all the answer provided so are are the same.. :(

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.