Braike dp
Braike dp

Reputation: 226

How to convert datetime stored in MySQL, to a different format using php

I have date stored in MySQL DB using CURRENT_TIMESTAMP as default, so that when a new data is inserted the date and time is stored automatically. And, I want to retrieve this data and display in my PHP. The retrived data looks like this 2019-03-17 15:02:36 .

Now I need to change it to March 17 2019 in my output.

I tried using date_format() but it is not displaying any output. It just returns blank data.

How can I achieve this.

Upvotes: 0

Views: 36

Answers (2)

Khurram Shaikh
Khurram Shaikh

Reputation: 300

Its quite simple use below code

SELECT *, DATE_FORMAT(YOURDATECOLUMN,'%d/%m/%Y') AS niceDate 
FROM table 
ORDER BY YOURDATECOLUMN DESC 

Fiddle : http://sqlfiddle.com/#!9/9eecb/82244

Upvotes: 1

Dave
Dave

Reputation: 5190

The DateTime functions of PHP to the rescue.

$sqldate = '2019-03-17 15:02:36';
$sqldate = DateTime::createFromFormat('Y-m-d G:i:s',$sqldate);
echo $sqldate->format('F d Y');

Produces:

March 17 2019

Upvotes: 1

Related Questions