3

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 ??

4 Answers 4

8

Use PHP's date function

date("Y-m-d",strtotime("19th April 2013")); 
Sign up to request clarification or add additional context in comments.

Comments

2

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 

Comments

1

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 

Comments

1

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'])); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.