Jump to:

23581 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Simple Date problem, help appreciated

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

Page: 1
Go to End
Author Topic: 909 Views
  • b0bro
    Avatar
    Community Member
    38 Posts

    Simple Date problem, help appreciated Link to this post

    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);
       }

  • b0bro
    Avatar
    Community Member
    38 Posts

    Re: Simple Date problem, help appreciated Link to this post

    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?

  • b0bro
    Avatar
    Community Member
    38 Posts

    Re: Simple Date problem, help appreciated Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Simple Date problem, help appreciated Link to this post

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

  • b0bro
    Avatar
    Community Member
    38 Posts

    Re: Simple Date problem, help appreciated Link to this post

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

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