0

Using Yii PHP framework, I produce the following query

SELECT * FROM `purchases` WHERE (date BETWEEN '2013-12-31' AND '2015-01-01')

with this code

$exportYear = "2014";
$criteria = new CDbCriteria();
$criteria->addBetweenCondition("date", ((int)$exportYear - 1) . "-12-31", ((int)$exportYear + 1) . "-01-01");
$purchases = Purchase::model()->findAll($criteria);

What I actually need is all 'purchases' that happened in year 2014. None from a minute before or after. What does the addBetweenCondition line need to be changed to in order to accomplish this?

2
  • why not just where year(date) = 2014? Commented Jan 7, 2015 at 14:39
  • @MarcB: That can't make use of indexes. Commented Jan 7, 2015 at 14:40

2 Answers 2

1

The BETWEEN operator is inclusive.

If column type is DATE use '2014-01-01' AND '2014-12-31'

$exportYear = "2014";
$criteria = new CDbCriteria();
$criteria->addBetweenCondition("date", $exportYear . "-01-01", $exportYear . "-12-31");
$purchases = Purchase::model()->findAll($criteria);

If column type is DATETIME use '2014-01-01 00:00:00' AND '2014-12-31 23:59:59'

$exportYear = "2014";
$criteria = new CDbCriteria();
$criteria->addBetweenCondition("date", $exportYear . "-01-01 00:00:00", $exportYear . "-12-31 23:59:59");
$purchases = Purchase::model()->findAll($criteria);
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT * FROM `purchases` 
WHERE date BETWEEN '2014-01-01' AND '2014-12-31'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.