Jump to content

Recommended Posts

Posted

Hi,

is there a faster/better way to do this?

With events containing startdate (mandatory) and enddate (optional) - I'm looking up what years has one or more events:

The result is a list of years that contains events. It works fine, but it's a bit slow.

<?for($i=2014;$i>=1980;$i--) {
   $date_start = new DateTime();
   $date_end = new DateTime();
   $date_start = $date_start->setDate($i, 1, 1);
   $date_end = $date_end->setDate($i+1, 1, 1);
   $date_start_ts = $date_start->getTimestamp();
   $date_end_ts = $date_end->getTimestamp();
   $e = $pages->get('/event')->numChildren("enddate!='',enddate>$date_start_ts,startdate<=$date_end_ts");
   $e2 = $pages->get('/event')->numChildren("enddate='',startdate>$date_start_ts,startdate<=$date_end_ts");
   $tot = $e+$e2;

   if($tot>0) {
      echo $i; //print year since it has events
   }
}
?>

tx, j

Posted

In this situation I would either cache the list of years using something like MarkupCache, or use the Date Archiver module to have an additional layer in the page tree for the years, which would be quick and easy to loop through.

  • Like 2

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
  • Recently Browsing   0 members

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