-2

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

Haris Hajdarevic
  • 1,535
  • 2
  • 25
  • 39
  • 1
    http://php.net/manual/en/function.strtotime.php and http://php.net/manual/en/function.date.php – AbraCadaver Oct 30 '15 at 21:45
  • `$mysql_date = date( 'Y-m-d H:i:s', strtotime( $date ) );` – Hasse Björk Oct 30 '15 at 21:50
  • 1
    Possible duplicate of [Convert from MySQL datetime to another format with PHP](http://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php) – Greg Oct 30 '15 at 22:10

3 Answers3

1

You can do it in PHP:

$mysql_date = date( 'Y-m-d H:i:s', strtotime( $date ) );
Hasse Björk
  • 1,431
  • 13
  • 19
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');
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
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");
Typel
  • 1,109
  • 1
  • 11
  • 34