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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Simple Date problem, help appreciated


Go to End


5 Posts   1393 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
bummzack

Community Member, 904 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 FROM_UNIXTIME(timestamp) 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...