首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据cakephp中的年份和月份检索数据

根据cakephp中的年份和月份检索数据
EN

Stack Overflow用户
提问于 2015-02-09 21:02:06
回答 1查看 3.4K关注 0票数 1

我正在尝试创建一个get语句函数,我在定义日期范围时遇到了一些问题。

数据将以以下格式通过get方法传递:?year=yyyy&month=mm表的相关列具有datetime类型(2012-02-01)。

我检查了TimeHelper类,特别是TimeHelper::daysAsSql($begin, $end, $fieldName, $timezone = NULL),但我不确定它是否适用于这种情况。

我在想,如果我能创建一个Y-m格式的日期,使用年份和月份这两个变量,然后在检索数据的条件下使用它,但我确信有一种更有效、更恰当的方法。有人能帮忙吗?

代码语言:javascript
运行
复制
$user_id = $Client['Client']['id'];
    $year = $this->request['url']['year'];
    $month = $this->request['url']['month'];
    //$date_created = date('Y-m');


  if (!empty($user_id) && (!empty($date_created))) {


        $Reports = $this->Report->find('all', array(
        'fields' => array('amount','currency','date_created','invoice_no'),
        'conditions' => array(
            'Report.client_id'=> $user_id,
            'Report.date_created >=' => $date_created, 
            'Report.date_created <=' => $date_created

             ),
        )
    );
EN

回答 1

Stack Overflow用户

发布于 2015-02-10 05:57:13

MONTH(dateColumn)返回一个整数,表示指定日期的月份。

YEAR(dateColumn)返回一个整数,表示指定日期的年份。

InCakephp使用作为

代码语言:javascript
运行
复制
$user_id = $Client['Client']['id'];
    $year = $this->request['url']['year'];
    $month = $this->request['url']['month'];
    //$date_created = date('Y-m');


    if (!empty($user_id) && (!empty($date_created))) {


        $Reports = $this->Report->find('all', array(
                'fields' => array('amount','currency','date_created','invoice_no'),
                'conditions' => array(
                        'Report.client_id '=> $user_id,
                        'MONTH(date_created ) >='=> $month,
                        'YEAR(date_created ) <=' => $year

                ),
        )
        );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28419282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档