The mysql database I set up used date format for the field. Consequently dates have to be entered as 0000-00-00. So when I retrieve the field name news_date from the database it will print as 2011-02-02. I would like to display it as February 2, 2011. Can someone help me with the code to make this happen? Thanks.

Recommended Answers

All 5 Replies

2011-02-02? Which one is the day and which is the month? :P
Assuming it's sql standard it's y-m-d

// assuming $sqldate came from the database
$tmpdate=explode("-",$sqldate);
$newtime=mktime(0,0,0,$tmpdate[1],$tmpdate[2],$tmpdate[0]);
$newdate=date("F j, Y",$newtime);

That's the long way around, I'm sure others know a shorter/quicker way but this should work.

David

SELECT DATE_FORMAT( curdate( ) , '%M %e, %Y' ) AS showdate

Mysql Format Date

or in php you can get by using,

date("F j, Y", strtotime($res['news_date']));

Here is the code I'm using and the date gets displayed as 2011-02-01 y-m-d:
How do I display as like February 3, 2011?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="CCS-Styles.css" rel="stylesheet" type="text/css" />
</head>

<body><div align="center" >
<table width="100%" border="0" cellspacing="3" cellpadding="8">
 

<?php 
$conn = mysql_connect('database', 'username', 'password'); 
if (!$conn) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully'; 
mysql_select_db(ndb);
$query  = "SELECT news_date, news_text FROM news ORDER BY ID desc";
$result = mysql_query($query);

while(list($news_date,$news_text)= mysql_fetch_row($result))
{
 Print "<tr><td class='ParaText' align='left' valign='top'>Date</td><td class='ParaText'>".$news_date ."</td><td class='ParaText' align='left' valign='top'>" .$news_text ."</td></tr>"; 
}
mysql_close($conn); 
?>
</div>
</body>
</html>

SELECT DATE_FORMAT( curdate( ) , '%M %e, %Y' ) AS showdate

Mysql Format Date

or in php you can get by using,

date("F j, Y", strtotime($res['news_date']));

I knew there was an easier way, just couldn't remember. Thanks.

Just replace the $next_date with php date function as below,

Print "<tr><td class='ParaText' align='left' valign='top'>Date</td><td class='ParaText'>".date("F j, Y", strtotime($news_date)) ."</td><td class='ParaText' align='left' valign='top'>" .$news_text ."</td></tr>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.