Manaus Posted January 12, 2023 Share Posted January 12, 2023 Hello, I am building a page of events, where I want to show only the coming ones. I tried this <?php $theNow = new DateTime(); // ora foreach ($page->children as $child): $firstDate = new DateTime(); $firstDate->setTimestamp( $child->event_when ); $interval = $firstDate->diff( $theNow ); echo $interval->s; // in seconds if ($interval >= 0) { // show article } ?> Beside returning a `Object of class DateInterval could not be converted to int` error, I ask if there is a method for displaying future events beside this. Thanks! Link to comment Share on other sites More sharing options...
wbmnfktr Posted January 12, 2023 Share Posted January 12, 2023 There was a similar question a few weeks back and we got it fixed as written here: In your case this would probably look a little bit more like this: // define today $today = strtotime("today"); // make $yesterday match the saved date format // in this case: dd.mm.yyyy (depends on your settings) $dateForOurSelector = date('d.m.Y', $today); // get our pages $events = $pages->find("template=eventTemplate, limit=7, eventDateField>$dateForOurSelector, sort=eventDateField"); foreach($events as $event) { // do whatever you want } Slightly different approach but should do the trick. 1 Link to comment Share on other sites More sharing options...
millipedia Posted January 12, 2023 Share Posted January 12, 2023 If you want to keep it super simple, you can also just use 'now' or 'today' to compare against your date field: $future_events=$page->children("event_when>'now'"); 3 Link to comment Share on other sites More sharing options...
Stefanowitsch Posted January 13, 2023 Share Posted January 13, 2023 13 hours ago, Manaus said: Hello, I am building a page of events, where I want to show only the coming ones. I am doing the exact thing in a project and this is how I get those events: // all future events from "today" $events = $page->children("date_event>=today, sort=date_event, sort=time_from, limit=6"); The magic lies here. You can literally "tell" the selector what to do. You don't need any timestamps or date functions. date_event>=today You could also do things like: // get all events of the current year $events = $pages->find("template=event, date_event>='first day of this year', date_event<'last day of this year', sort=date_event, sort=time_from, limit=6"); 4 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now