Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject compare date

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

Page: 1
Go to End
Author Topic: 2635 Views
  • Megabyte
    Avatar
    Community Member
    13 Posts

    DataObject compare date Link to this post

    Hello,

    I creating a event-list module. Now I want to show the next five events on every page. But I have a problem to filter only events in future. This way does not work:

    function NextEvents($num=5) {
       $timestamp=time();
       $datetoday=date("Y-m-d",$timestamp);
    $events = DataObject::get_one("eventholder");
    return ($events) ? DataObject::get("event", "date > $datetoday", "", $num) : false;

    How can I compare the event-date with the current date?

  • Megabyte
    Avatar
    Community Member
    13 Posts

    Re: DataObject compare date Link to this post

    No idea??
    The query works perfectly for all records. But how can I get only records with a date in future?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: DataObject compare date Link to this post

    The where clause is MySQL. MySQL has a function CURDATE() that returns the current date, so you don't need to generate it yourself. Also, 'date' is a reserved sql word, so you'll need to escape it with backticks.

    This should work (not tested - but refer to MySQL documentation for further info):

    return ($events) ? DataObject::get("event", "`date` > CURDATE()", "", $num) : false;

  • Megabyte
    Avatar
    Community Member
    13 Posts

    Re: DataObject compare date Link to this post

    Thanks for the answer, but it does not work. There is still the error "Couldn't run query [...] right syntax to use near '5 WHERE (`date` > CURDATE()) AND (`SiteTree_Live`.ClassName IN ('event')) GROUP' at line 1"

    How does it work??

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: DataObject compare date Link to this post

    return ($events) ? DataObject::get("event", "`date` > CURDATE()", "", $num) : false;

    should be changed to

    return ($events) ? DataObject::get("event", "`date` > CURDATE()", "", "", $num) : false;

  • Megabyte
    Avatar
    Community Member
    13 Posts

    Re: DataObject compare date Link to this post

    THANKS!!!! It works great!!!!!!!!!! THANKS!!!

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