Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » get dates later than today

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

Page: 1
Go to End
Author Topic: 829 Views
  • Bruce B
    Avatar
    Community Member
    145 Posts

    get dates later than today Link to this post

    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

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: get dates later than today Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: get dates later than today Link to this post

    I think you can still use where, something like:

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

    Martine

  • Bruce B
    Avatar
    Community Member
    145 Posts

    Re: get dates later than today Link to this post

    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!

    829 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.