我正在尝试从数据库中显示日期和时间。时间问题在于它总是在改变它的值并且与存储在数据库中的值不匹配。
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
答案 0 :(得分:2)
在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)
答案 1 :(得分:0)
试试这个,您需要使用$posttime
变量而不是$time
$date = date("D d S M Y", strtotime($post['post_date']));
$posttime = date("g:ia", strtotime($post['post_date']));
echo $date .' at ' . $posttime;
输出:
$date = date("D d S M Y", strtotime('2014-01-28 04:00:00'));
$posttime = date("g:ia", strtotime('2014-01-28 04:00:00'));
echo $date .' at ' . $posttime;
//output: Tue 28 th Jan 2014 at 4:00am