21280 Posts in 5729 Topics by 2600 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 798 Views |
-
Simple Date problem, help appreciated

29 September 2009 at 6:03pm Last edited: 29 September 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);
} -
Re: Simple Date problem, help appreciated

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?
-
Re: Simple Date problem, help appreciated

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 -
Re: Simple Date problem, help appreciated

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). -
Re: Simple Date problem, help appreciated

29 September 2009 at 8:38pm
thanks banal, thats something i didnt think of! I might have a crack at it tomorrow...
| 798 Views | ||
|
Page:
1
|
Go to Top |


