Manoj K
Manoj K

Reputation: 389

convert date to Mysql date format php

I have this date format in my db

19th April 2013

I want to convert it to

2013-04-19

I have used if,else conditions , BUt it becomes too long and complicated .Are there any built in functions ??

Upvotes: 3

Views: 7458

Answers (4)

liyakat
liyakat

Reputation: 11853

yes below code to change date format

date("Y-m-d",strtotime("19th April 2013")); to get as 2013-04-19

and for second one

    date("Y/m/d",strtotime("17th April 2013")); to get as 2013-04-17

Upvotes: 2

Shafqat Masood
Shafqat Masood

Reputation: 2570

following PHP code convert the date into desired format

 $date = '19th April 2013'
 echo date('Y-m-d', strtotime($date));

and other formats are as follows

April 22, 2013  date("F d, Y",strtotime($myrow['date']));
Monday, April 22, 2013  date("l, F d, Y",strtotime($myrow['date']));
Apr 22, 2013    date("M d, Y",strtotime($myrow['date']));
22 April 2013   date("d F Y",strtotime($myrow['date']));
22 Apr 2013 date("d M Y",strtotime($myrow['date']));
Mon, 22 Apr 2013    date("D, d M Y",strtotime($myrow['date']));
Monday, the 22nd of April, 2013 date("l",strtotime($myrow['date'])) . ", the " .   date("jS",strtotime($myrow['date'])) . " of " . date("F, Y",strtotime($myrow['date']));

Upvotes: 1

John Woo
John Woo

Reputation: 263723

You can still do it via query so you will not have any conversion on PHP side. Since the data type of the column is string, you need to convert it to a valid date using STR_TO_DATE.

SELECT DATE_FORMAT(STR_TO_DATE(columnname, '%D %M %Y'), '%Y-%m-%d') new_format
FROM   TableName

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Use PHP's date function

date("Y-m-d",strtotime("19th April 2013"));

Upvotes: 8

Related Questions