19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHPのfindメソッドのconditionsオプションキーで日付をうまく使う

Last updated at Posted at 2015-09-30

ある日付を指定して、
日別の集計等をしたいけど、
持っている日付データはdatetime(timestamp)のみのときの話です。

// 対象の日付
$targetDate = '2015-09-30'

2015-09-30のデータだけほしいのですが、
まずこんな感じだとダメです。(createdカラムはdatetime型)

'conditions' => array(
	'created' => $targetDate
)

datetime型のcreatedに対してdate型のものと同じかどうかを比較しているのでダメです。

仕方なく、こんな感じでダサく書いてました。

'conditions' => array(
	'created >= ' => $targetDate,
	'created < ' => date('Y-m-d', strtotime("$targetDate +1 day"))
)

これは、こんな感じでbetweenをうまく使って書けます。

'conditions' => array(
	'created between ? and ?' => array($targetDate.' 00:00:00', $targetDate.' 23:59:59'),
)
19
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?