php - 如果模型 'created' 的字段 'listings' 中的日期超过 2 周前,如何在 cakephp 中查询?

标签 php mysql cakephp

我有一个模型“列表”,其中包含一个日期时间格式的“创建”字段。 我需要在 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/

相关文章:

mysql - 如何在mysql查询中总计 "AS"列?

cakephp - 对于大型项目来说,CakePHP 和纯 PHP 哪个更好?

javascript - 简化关联数组

javascript - 在网页中模拟 DOS 或终端屏幕的最佳方法是什么?

javascript - 地理定位 - 检查某个位置是否属于某个区域

php变量知道jquery何时加载?

mysql - 无法安装 MySQL - libaio.so.1 : cannot open shared object file: No such file or directory - Live Installation with Persistence

php - 是否可以将 HTML 表中的数据发布到另一个 PHP 页面?

android - 从 mySQL 服务器获取数据时让 ListView 工作

mysql - 在 CakePHP 中计算新的平均 afterSave(或更好的东西?)