Convert date to MySQL format date

时间:2015-10-30 21:43:59

标签: php mysql

How to convert date like this: Oct 28, 2015 12:08:59 AM

To MySQL format date?

I take a look onto this http://php.net/manual/en/datetime.createfromformat.php but that wasn't helpful for me.

Thanks

3 个答案:

答案 0 :(得分:1)

You can do it in PHP:

$mysql_date = date( 'Y-m-d H:i:s', strtotime( $date ) );

答案 1 :(得分:0)

You can do it directly in MySQL.

SELECT STR_TO_DATE('Oct 28, 2015 12:08:59 AM','%b %d, %Y %I:%i:%s %p');

答案 2 :(得分:0)

PHP's DateTime class makes it easy to convert any date format to another. http://php.net/manual/en/class.datetime.php

You start by reading your date/time into a new DateTime object using its member function createFromFormat. This function's first argument accepts the same date formats as the date() function does, so they should already be pretty familiar. If not, these are all very well documented on php.net.

$YourDate = "Oct 28, 2015 12:08:59 AM";
$FormattedDate = DateTime::createFromFormat("M j, Y H:i:s A", $YourDate );

Next, you can simply use DateTime's format function to output the date in any format you need; in this case, I'm assuming MySQL's DateTime format which follows Y-m-d H:i:s format:

$FormattedDate = $FormattedDate->format("Y-m-d H:i:s");