我正在尝试显示数据库中的日期和时间。时间的问题是它总是在改变它的值并且与数据库中存储的值不匹配。
PHP 代码:
$query = mysql_query("SELECT `post_date`
FROM `private_section_posts`
WHERE `status` = 'show'");
while($post = mysql_fetch_array($query)) {
$postdayofweek = date("D", strtotime($post['post_date']));
$postday = date("d", strtotime($post['post_date']));
$postsuffix = date("S", strtotime($post['post_date']));
$postmonth = date("M", strtotime($post['post_date']));
$postyear = date("Y", strtotime($post['post_date']));
$posttime = date("g:ia", strtotime($post['post_date']));
echo $postdayofweek . ' ' . $postday . $postsuffix . ' ' . $postmonth . ' ' . $postyear .
' at ' . $time;
}
MYSQL post_date: 2014-01-28 04:00:00
输出显示:2014 年 1 月 28 日星期二上午 10:36
最佳答案
在MySQL中你可以这样尝试
SELECT DATE_FORMAT(post_date,'%a %D %b %Y at %l:%i%p') FROM `private_section_posts` WHERE `status` = 'show'
示例
SELECT DATE_FORMAT(NOW(),'%a %D %b %Y at %l:%i%p');
+---------------------------------------------+
| DATE_FORMAT(NOW(),'%a %D %b %Y at %l:%i%p') |
+---------------------------------------------+
| Tue 28th Jan 2014 at 5:19PM |
+---------------------------------------------+
1 row in set (0.00 sec)
关于mysql - 显示 mysql 数据库 DATETIME 中的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404613/