Jump to content

Displaying only future events


Manaus
 Share

Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

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

 

  • Like 4
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...