Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Date Filter Difficulties

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

Page: 1
Go to End
Author Topic: 1813 Views
  • Laurie
    Avatar
    Community Member
    21 Posts

    Date Filter Difficulties Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Date Filter Difficulties Link to this post

    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)

  • Laurie
    Avatar
    Community Member
    21 Posts

    Re: Date Filter Difficulties Link to this post

    Worked perfectly...thanks so much. Cheers.

  • ttyl
    Avatar
    Community Member
    114 Posts

    Re: Date Filter Difficulties Link to this post

    worded for me too.

    1813 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.