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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Date Filter Difficulties


Go to End


4 Posts   3105 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, 5523 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