CakePHP:如何根据创建时间从数据库中获取数据


CakePHP: how to fetch data from the database based on created time

我正在制作一个类似Facebook的网站。为此,我想实现"长轮询"以使用新帖子动态更新用户墙。

这是我计划的伪代码

//when user first open my website
loadPostsNormally();
var lastTime = current_time;
function update(){
$.getJSON('/some_other_page_which_will_fetch_the_contents_created_after_lastTime_and_encode_it_to_json/'+lastTime,function(){
lastTime = current_time;
showNewPostsOnWall();
}
setInterval(update,2000);

现在问题出在我的页面some_other_page_which_will_fetch_the_contents_created_after_lastTime_and_encode_it_to_json

public function some_other_page_which_will_fetch_the_contents_created_after_lastTime_and_encode_it_to_json($timestamp){
    //fetch_all_data_created_after_this_timestamp_but_how ??
}

我想从我的帖子模型中获取,而我的"创建"帖子表中的时间就像2014-11-19 22:34:09

很抱歉使用非常外行的语言来描述问题。我使用它是因为我还没有开始它,我只是在脑海中制定一个解决这个问题的计划。

o 只是获取数据,很简单 只是使用条件:

$results = $this->Model->find('all',array(
                            'conditions'=>array('Model.date_creation >'=>  '2014-11-20'),
                            'recursive'=>1)
                    );

如何根据时间间隔获取数据?

所以你有$start和$end。

$start = '1800-01-01 00:00:00' //Januuary 1, 1800 or whatever you choose
$end = date('Y-m-d H:i:s'); // Now
SELECT *
FROM `Posts` p
WHERE (
p.data_created
BETWEEN $start
AND $end
)

甜!现在你可以整天取货了。