Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:43:47+00:00 2026-06-13T09:43:47+00:00

I am having trouble getting any find operations to work using the SQL function

  • 0

I am having trouble getting any find operations to work using the SQL function NOW() in my conditions.

I am effectively trying to build a find query that says:

Desired SQL:

WHERE (NOW() BETWEEN Promotion.start AND Promotion.end) AND Promotion.active = 1

I have tried many combinations but no matter what I do when using NOW() in the condition, it doesn’t work because the query Cake builds puts ' quotation marks around the model fields so they are interpreted by MySQL as a string.

$this->find('all', array(
    'conditions' => array(
        '(NOW() BETWEEN ? AND ?)' => array('Promotion.start', 'Promotion.end'),
        'Promotion.active' => 1
    )
));

CakePHP created SQL:

Notice the single quotes around the model fields in the BETWEEN(), so they are treated as strings.

WHERE (NOW() BETWEEN 'Promotion.start' AND 'Promotion.end') AND `Promotion`.`active` = '1'

This doesn’t work either.

$this->find('all', array(
    'conditions' => array(
        'NOW() >=' => 'Promotion.start',
        'NOW() <=' => 'Promotion.end',
        'Promotion.active' => 1
    )
));

I know why these solutions don’t work. It’s because the model fields are only treated as such if they are the array key in the conditions, not the array value.

I know I can get this to work if I just put the whole BETWEEN() condition as a string:

$this->find('all', array(
    'conditions' => array(
        'NOW() BETWEEN Promotion.start AND Promotion.end',
        'Promotion.active' => 1
    )
));

Another example of the same problem is, which is simpler to understand:

Desired SQL:

WHERE Promotion.start > NOW() AND Promotion.active = 1

So I try this:

$this->find('all', array(
    'conditions' => array(
        'Promotion.start >' => 'NOW()',
        'Promotion.active' => 1
    )
));

And again it doesn’t work because Cake puts ' quotations around the NOW() part.

CakePHP created SQL:

WHERE `Promotion`.`start` > 'NOW()' AND `Promotion`.`active` = '1''
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:43:47+00:00Added an answer on June 13, 2026 at 9:43 am

    Better to not use NOW() as its a function and functions don’t use indexes. A better solution would be:

    $this->find('all', array(
        'conditions' => array(
            "'" . date('Y-m-d') . "' BETWEEN Promotion.start AND Promotion.end",
            'Promotion.active' => 1
        )
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting my callback function to work in post: I've got this
I'm having trouble getting the now.js chat client tutorial to work. (I've also followed
I'm having trouble getting any information to display from this query. Anyone know where
I'm having trouble finding/getting any logging output from a web service. I have a
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
Im having trouble getting my JTable that im using to display either check boxes
I'm having trouble getting the CanExecute method of my command to work property. I've
I'm having trouble getting Tire working using ElasticSearch with the Bonsai addon on the
I'm currently having trouble getting example code for using tweepy to access Twitter's Streaming
I'm having trouble getting Japanese text to display properly on a website compiled using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.