SQL
/* The SQL syntax I used to create my 'time' field:
ALTER TABLE `files` ADD `time` DATE NOT NULL AFTER `id`;
*/
然后我将函数"NOW()"添加到我想要的值中。
.PHP
<?php
// Selects all the files from my table 'files'. It contains 'title', 'content', 'time'.
$get_files = mysql_query("SELECT * FROM
files ORDER BY id
DESC LIMIT 0, 3", $connection);
echo "<div class='display-files'>"; // Wraps this around the Article.
while($files = mysql_fetch_array($get_files)) {
$title_get = $files['title']; // Gets the 'title' from table 'files'.
$content_get = $files['content']; // Gets the 'content' from table 'files'.
$date_get = $files['date']; // Gets the 'date' from table 'files'.
echo "<article>"; // Article is just to organize the content.
echo "<h2>{$title_get}</h2>"; // Displays 'title'.
echo "<p style='white-space: pre-line'>{$content_get}</p>"; // Displays 'content'.
echo "<hr /><p>Created: <em>{$date_get}</em><br /></p>"; // Displays 'date'.
echo "</article>";
}
echo "</div>";
?>
变量应该解释得很好,但我添加了评论......以防万一那些在阅读我的代码时遇到麻烦的人。另外,我正在做的第一个问题。
结果 : 2016-03-14
无论如何,我在这里问是否有办法从我的数据库/数据库中获取"时间"并为其提供良好的可读格式。
例如:2016年3月14日
尝试这样的事情:
$date_get = $files['date']; // "2016-03-14"
echo date('M d Y',strtotime($date_get)); // Mar 14 2016
您可能想看看这个链接: PHP 日期时间函数
希望这有帮助。
和平!
您可以在选择列表中添加 DATE_FORMAT():
SELECT ..., DATE_FORMAT(`date`, '%b %d %Y') AS `date`
FROM files ORDER BY id
DESC LIMIT 0, 3
下面是格式效果的演示:
mysql> SELECT DATE_FORMAT('2016-03-14', '%b %d %Y');
+---------------------------------------+
| DATE_FORMAT('2016-03-14', '%b %d %Y') |
+---------------------------------------+
| Mar 14 2016 |
+---------------------------------------+
1 row in set (0.00 sec)