Reputation: 187
I have a 'date' field in a MySQL table - its structure is 'DATE'. When I echo this out it displays as the standard year-month-day e.g. 2013-04-23. How do I alter the PHP to display it as Day-Month-Year and preferably with the month name displayed e.g. 23-April-2013.
this is the query
$sql = 'SELECT headline, story, date, name
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id ORDER BY date DESC' ;
$result = $pdo->query($sql);
this is the foreach loop
foreach ($result as $row)
{
$news[] = array(
'headline' => $row['headline'],
'story' => $row['story'],
'date' => $row['date'],
'name' => $row['name']
);
}
And this is the PHP code currently used to display the date.
<?php echo $new['name'] , ' ', $new['date']; ?>
How to I go about making this happen? Thanks
Upvotes: 0
Views: 321
Reputation: 4568
This is how to do it in PHP, the other suggestions may be better as they do it directly in MySQL. Depends what you want I guess.
$fixed_date = date('d-F-Y',strtotime($new['date']));
Should do it.
Upvotes: 0
Reputation: 219814
Use MySQL's DATE_FORMAT()
function to format your date in your query:
SELECT
DATE_FORMAT(date, "$e-%M-%Y") AS date
Upvotes: 3
Reputation: 7025
MySQL can do this work for you, using DATE_FORMAT()
https://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_date-format
DATE_FORMAT( date , '%d-%M-%Y' )
Upvotes: 2