2011-10-26 39 views
0

我需要創建一個函數,只顯示當前日期的文章。 這裏是我創造:Php僅獲取今日文章?

function lastnews() { 
    $id = mysql_real_escape_string ($id); 
    $date = date('d'); 
    $sql = 'SELECT * FROM posts WHERE post_status = "publish" AND post_date = "$date" ORDER BY post_date DESC LIMIT 3'; 
    $res = mysql_query($sql) or die (mysql_error()); 

if (mysql_num_rows($res) !=0): 
    while ($row = mysql_fetch_assoc($res)) { 


    $title = $row['post_title']; 
    echo '<li><a href="post.php?id='.$row['id'].'">'.$title.'</a></li>';  
} 
    endif; 

} // end 

但是,這是行不通的。日期在數據庫中的formt是:

Y-m-d H:i 

感謝您閱讀本

回答

3

你需要指定整個日期(date('Y-m-d'))和查詢應該說AND post_date > '$date'因爲它包含的時間。你也可以說AND post_date > CURRENT_DATE

+0

謝謝你的回答。 – Meo

+0

這很快。我在想同樣的事情,但檢查語法:-( - 會提示這也是:'... LIKE'_ $ date _%''。 – Smamatti

1

更改 $ date = date('d');

收件人: $ date = date('Y-m-d H:i');