Use of date function in PHP to output a user-friendly date
I have a MySQL database column named DateAdded.
I'd like to echo this as a readable date/time. Here is a simplified version of the code I currently have:$result = mysql_query("
SELECT ListItem, DateAdded
FROM lists
WHERE UserID = '" . $currentid . "'
");
while($row = mysql_fetch_array($result))
{
// Make the date look nicer
$dateadded = date('d-m-Y',$row['DateAdded']);
echo $row['ListItem'] . ",";
echo $dateadded;
echo "<br />";
}
Is the use of th开发者_Python百科e date function the best way to output a user-friendly date?
Thanks for taking a look,
If you don't plan on outputting dates beyond 2038, you will be fine using date()
.
Otherwise, use PHP's DateTime
which doesn't have that limitation, or mySQL's date formatting functions to format the date directly in the database.
However, you seem to be storing a timestamp in the database. Have you considered switching to a DATETIME
field?
It is fine to use date()
to show users.
Is there any reason you are not using the DATE
type in MySQL?
I sometimes used MySQL's DATE_FORMAT().
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
精彩评论