只获得今天的帖子?

时间:2011-10-27 16:58:01

标签: mysql php

我需要一个能够回应今天发布的帖子的功能。

从00.00到23.59。我已经创建了一个函数,但它没有给出我需要的结果 这是我的功能:

function todayNews() {
    $id = mysql_real_escape_string ($id);
    $sql = 'SELECT * FROM wp_posts WHERE  post_status = "publish" AND post_date = CURRENT_DATE  ORDER BY post_date DESC LIMIT 15';
    $res = mysql_query($sql) or die (mysql_error());

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

    $title = ($row['post_title']);

    $old_date = $row['post_date'];             // returns Saturday, January 30 10 02:06:34
    $old_date_timestamp = strtotime($old_date);
    $new_date = date('H:i', $old_date_timestamp);

    $mycontent = ($row['post_content']);
    $mycontent = strip_tags($mycontent);
    $mycontent = substr($mycontent,0,350);
    $mycontent = preg_replace("/\[caption.*\[\/caption\]/", '', $mycontent); 

    $first_img = '';
    $my1content = $row['post_content'];
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
    $first_img = "/img/default.png";
    }

    echo '


    <ul class="li-sub">
                <li>
'.$title.'
</li>

        </ul>

    ';  
}
    else:
        echo 'There are no posts for today !';
    endif;

} // end  

谢谢!

编辑: Post_date具有以下格式:Y-m-d H:i

1 个答案:

答案 0 :(得分:2)

您正在将POST_DATE(日期时间)与CURRENT_DATE(日期)进行比较,它不会给出任何结果。

您可以尝试以下SQL

$sql = 'SELECT * FROM wp_posts WHERE  post_status = "publish" AND DATE(post_date) = CURRENT_DATE  ORDER BY post_date DESC LIMIT 15';

这会在比较当前日期之前将post_date转换为仅日期。