Harts
Harts

Reputation: 4093

Cakephp How to order by date and month only

I have a contact table with date field for birthdate.

Now I would like to show list of birthday within next week. I can show it, but the order is based on the year.

How can I order the data, just based on month and day only?

$sideBarTaskSuggestion = 
$this->Project->Contact->find('all', array(
   'conditions' => array(
      'Contact.group_id' => $this->Session->read('Auth.User.group_id'),
      'Contact.birthdate NOT' => null,
      'AND' => array(
           array('Contact.birthdate NOT' => null),
           array('Contact.birthdate + INTERVAL EXTRACT(YEAR FROM NOW()) - 
              EXTRACT(YEAR FROM Contact.birthdate) YEAR <=' => date('Y-m-d', 
                 strtotime('+1 week'))),
           array('Contact.birthdate + INTERVAL EXTRACT(YEAR FROM NOW()) -
              EXTRACT(YEAR FROM Contact.birthdate) YEAR >=' => date('Y-m-d')),
      )
    ),
   'order' => 'Contact.birthdate DESC'
)
);

Upvotes: 2

Views: 2161

Answers (1)

AgRizzo
AgRizzo

Reputation: 5271

When you say you want 'order the data, just based on month and day only?', I assume you mean:

  • Nov 22, 2013
  • Oct 21, 1922
  • Mar 5, 2000
  • Jan 31, 2001
  • Jan 1, 1990

Then try this: 'order' => 'DAYOFYEAR(Contact.birthdate) DESC'

Upvotes: 4

Related Questions