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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

get dates later than today


Reply


4 Posts   1019 Views

Avatar
Bruce B

Community Member, 147 Posts

8 January 2013 at 5:34pm

I hate it when simple things trip you up. In my SS2.4 site I had the following line to collect all future events pages:

$littlechildren = DataObject::get("ArticlePage", "ParentID = $this->ID and Date >= CURDATE()", "Date ASC", null, null);

In SS3, I have this:

$holder = ArticleHolder::get()->filter('URLSegment', 'meetings')->first();
return ($holder) ? ArticlePage::get()->filter(array('ParentID' => $holder->ID,'Date:GreaterThan' => '2013-01-07'))->sort('Date DESC') : false;

I'd like to replace the string '2013-01-07' with CURDATE() but SS3 does not like CURDATE() in that context and considers it an undefined function.

What's the best solution?

cheers
bruce

Avatar
copernican

Community Member, 189 Posts

9 January 2013 at 1:40am

Edited: 10/01/2013 2:06am

Maybe not the best solution, but you could use PHP date function to figure out the current date and use it in place of your hard coded "2013-01-07", something like

$curDate = date("Y-m-d");

$holder = ArticleHolder::get()->filter('URLSegment', 'meetings')->first();
return ($holder) ? ArticlePage::get()->filter(array('ParentID' => $holder->ID,'Date:GreaterThan' => $curDate))->sort('Date DESC') : false;

edit* - added missing quotes around (Y-m-d). Thanks for pointing out Bruce.

Avatar
martimiz

Forum Moderator, 1132 Posts

9 January 2013 at 5:24am

I think you can still use where, something like:

ArticlePage::get()->filter(array('ParentID' => $holder->ID))->where("Date >= CURDATE()")->sort('Date DESC');

Martine

Avatar
Bruce B

Community Member, 147 Posts

9 January 2013 at 7:13pm

I can now report that both methods work just fine. I think I'll go with IOTI's version as it seems a little more in keeping with the SS 3 'vibe'. The only change required is that the first line should include quotes:

$curDate = date("Y-m-d");

I'll file martimiz's version away for future problems.

Thanks heaps guys for the quick responses!