我有一个模型“列表”,其中包含一个日期时间格式的“创建”字段。 我需要在 View 中列出两周前创建的所有列表。 如果可能的话,额外的一件事是以某种方式将它们标记为过期。
这是在 cakePhp 1.27 中
最佳答案
嗨,我认为你可以使用一个简单的脚本在蛋糕中做到这一点。
function meScript(){
// first load your model if necessary
$listingModel = ClassRegistry::init('Listing');
// Then set your date margin to , two weeks back
$date_margin = date("Y-m-d H:i:s", strtotime('-2 week')) ;
// now retrieve all records that were created over 2 weeks ago
$listings = $listingModel ->find('all', array(
'conditions' => array('created <' => $date_margin),
)
);
}
差不多就这些了。由于 margin 日期采用“Y-m-d H:i:s”格式,因此“'created <' => $date_margin”条件将检索在该日期之前创建的所有记录。
至于下一步将它们标记为过期: 只需循环遍历结果并使用它们的 id 将“过期”字段(或数据库表中的任何名称)设置为“true”即可。
关于php - 如果模型 'created' 的字段 'listings' 中的日期超过 2 周前,如何在 cakephp 中查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2894938/