如何将日期过滤器添加到mysql。的Joomla

时间:2012-12-18 13:35:53

标签: php mysql joomla

<?php
        $database = & JFactory::getDBO();
        $query = "SELECT asset_id FROM #__content WHERE state = 1";
        $database->setQuery( $query );
        $count_at = $database->loadObjectList();
        $count_nbr = count($count_at);
?>

所以$count_nbr会返回所有创建和发布的文章,但我需要添加“今天”过滤器。如果有些大师可以请...请需要2种类型的过滤器:“1天”和“周”...

更新:将$date = date (Y-m-d); amd modifieng字符串添加到SELECT asset_id FROM #__content WHERE state = 1 AND publish_up = $date;并没有帮助导致mysql中的publish_up Y-m-d H:i:s我需要忽略时间戳......

1 个答案:

答案 0 :(得分:1)

获取今天发表的文章:

$today = date('Y-m-d');
$query = "SELECT asset_id FROM #__content WHERE state = 1 AND DATE_FORMAT(publish_up, '%Y-%m-%d') = '$today'";
...

这使用MySQL DATE_FORMAT()函数将发布日期格式化为Y-m-d格式,以便您可以对其进行比较。

要获取本周发布的文章,请使用YEARWEEK()函数:

$query = "SELECT asset_id FROM #__content WHERE state = 1 AND YEARWEEK(publish_up) = YEARWEEK(NOW())";