开发者

Selecting new records by datetime

I'm trying to select records which have a recdate within the past year

$goodate = date('Y-m-d h:i:s', mktime(0, 0, 0, date开发者_如何学C("m"),   date("d"),   date("Y")-1));
$sqlstmt = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate.'"' ;

but I'm getting no records.

What am I doing wrong?


$sqlstmt = "SELECT * FROM #__mytable
            WHERE id=".$uid."
              AND recdate > ADDDATE(CURDATE(), INTERVAL -1 YEAR)";


If you want to do this programmatically, the DateTime object is a great tool.

       $date = new DateTime();
       $gooddate = $date->sub(DateInterval::createFromDateString('1 year'));
       $sql = "SELECT * FROM #__mytable WHERE id=".$uid." AND recdate > ".$goodate->format('Y-m-d').'"';

This is assuming your date field is mysql type DATE. If not, there are similar DateTime methods for outputting unix timestamps, and the format() method accepts any formatting string that date() does.


$sql = "
select * 
from #__mytable
where 
    id = $uid
    and 
    date_format(recdate, '%Y') = date_format(adddate(curdate(), interval -1 year), '%Y')
"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜