Reputation: 276
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
Upvotes: 1
Views: 1968
Reputation: 14479
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.
Upvotes: 2
Reputation: 6877
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
?>
Upvotes: 5
Reputation: 163
firstly you need to save it as Timestamp so,
$startDate = strtotime($javascript_value);
$result = date("Y-m-d H:i:s",$startDate);
Upvotes: 0