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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

DataObject compare date


Go to End


6 Posts   4322 Views

Avatar
Megabyte

Community Member, 13 Posts

1 March 2009 at 5:17am

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?

Avatar
Megabyte

Community Member, 13 Posts

2 March 2009 at 3:02am

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

Avatar
Hamish

Community Member, 712 Posts

2 March 2009 at 8:31am

Edited: 02/03/2009 8:31am

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;

Avatar
Megabyte

Community Member, 13 Posts

2 March 2009 at 9:10am

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??

Avatar
Sam

Administrator, 690 Posts

2 March 2009 at 10:22am

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

should be changed to

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

Avatar
Megabyte

Community Member, 13 Posts

2 March 2009 at 10:51pm

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