2014-01-28 82 views
0

我想從數據庫中顯示日期和時間。時間問題在於它總是在改變它的值,並且與存儲在數據庫中的值不匹配。從MySQL數據庫顯示時間DATETIME

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年1月28日04:00:00

輸出顯示:週二2014年1月28日在上午10:36

+0

如果你存儲正確的時間,然後只是顯示它是一個子串時間字段。 –

回答

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; 

OUTPUT:

$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 
+0

仍然有相同的問題 – glennjgalea

+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)