我正在尝试创建一个get语句函数,我在定义日期范围时遇到了一些问题。
数据将以以下格式通过get方法传递:?year=yyyy&month=mm表的相关列具有datetime类型(2012-02-01)。
我检查了TimeHelper类,特别是TimeHelper::daysAsSql($begin, $end, $fieldName, $timezone = NULL)
,但我不确定它是否适用于这种情况。
我在想,如果我能创建一个Y-m格式的日期,使用年份和月份这两个变量,然后在检索数据的条件下使用它,但我确信有一种更有效、更恰当的方法。有人能帮忙吗?
$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
),
)
);
发布于 2015-02-09 21:57:13
MONTH(dateColumn)返回一个整数,表示指定日期的月份。
YEAR(dateColumn)返回一个整数,表示指定日期的年份。
InCakephp使用作为
$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
),
)
);
https://stackoverflow.com/questions/28419282
复制