在PHP中使用日期函数输出用户友好的日期
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 the date function the best way to output a user-friendly date?
Thanks for taking a look,
我有一个名为DateAdded的MySQL数据库列。
我想将此作为可读日期回显 / time。
这是我目前拥有的代码的简化版本: p>
$ result = mysql_query(“
SELECT ListItem,DateAdded
FROM lists \ n WHERE UserID ='“。$ currentid。”'
“);
while($ row = mysql_fetch_array($ result))
{
//使日期看起来更漂亮
$ dateadded = date('dm -Y',$ row ['DateAdded']);
echo $ row ['ListItem']。 “,”;
echo $ dateadded;
echo“&lt; br /&gt;”;
}
code> pre>
使用日期函数是 输出用户友好日期的最佳方式是什么? p>
谢谢你看看, p>
div>
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