Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Date Filter Difficulties


Reply


4 Posts   1931 Views

Avatar
Laurie

Community Member, 21 Posts

28 May 2010 at 12:05pm

I have a series of news items saved in my database, each with a startDate and endDate. I'm trying to figure out how to write the filter condition to get all current news item (i.e., those where startDate <= Now and endDate > Now. I've looked around for an example to emulate, but without success.

Here's what I have so far...

class HomePage_Controller extends Page_Controller {
   function HomePageNews() {
      return DataObject::get('NewsItem', [date filter goes here], 'ID DESC');
   }
}

Any assistance on writing the date filter would be most appreciated. Thanks.

Cheers,
Laurie

Avatar
Willr

Forum Moderator, 5513 Posts

28 May 2010 at 1:27pm

The date fields are date time stamps so you can do something like the following... should work

$now = date('Y-m-d H:i:s');

$where = "startDate <= '$now' AND endDate > '$now'";

I think should work fine (but haven't tested it)

Avatar
Laurie

Community Member, 21 Posts

28 May 2010 at 1:51pm

Worked perfectly...thanks so much. Cheers.

Avatar
ttyl

Community Member, 114 Posts

19 August 2010 at 7:11am

worded for me too. :D