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

Simple Date problem, help appreciated


Reply


5 Posts   968 Views

Avatar
b0bro

Community Member, 38 Posts

29 September 2009 at 6:03pm

Edited: 29/09/2009 6:21pm

How do I select last 4 events that end 7 days in the past. Ie they stay on the homepage 7 days after the actual event.
This dosnt work, its returning all of them! am i on the right track??

function UpcomingEvents(){   

      
      $lastweek = mktime(0, 0, 0, date("m"), date("d")-7, date("Y"));
      Debug::message($lastweek);

      return DataObject::get("EventPage", "`EventPage`.`Date` > '{$lastweek}'", "Date DESC", "", 4);
   }

Avatar
b0bro

Community Member, 38 Posts

29 September 2009 at 6:14pm

just noticed that i want the date in the future not the past. So changed the -7 to a +7. However my problem still remains?

Avatar
b0bro

Community Member, 38 Posts

29 September 2009 at 7:53pm

i sorted it out, for future reference

return DataObject::get("EventPage", "DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= `EventPage`.`Date`", "Date DESC", "", 4);

also see,
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Avatar
banal

Community Member, 901 Posts

29 September 2009 at 8:07pm

Yes, that's a good approach, although it is MySQL specific. If you ever change the DB, your code will probably break. Note an issue though, it's probably not going to happen :)

Just to answer your original question:
It doesn't work because you compare a UNIX timestamp to a MySQL Date field. Either you first format the $lastweek timestamp as SQL Date and then use it in the query, or you use [url=http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime]FROM_UNIXTIME(timestamp)[/url] in your query (which is most likely MySQL only again).

Avatar
b0bro

Community Member, 38 Posts

29 September 2009 at 8:38pm

thanks banal, thats something i didnt think of! I might have a crack at it tomorrow...