joe_g Posted June 22, 2014 Share Posted June 22, 2014 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 Link to comment Share on other sites More sharing options...
Craig Posted June 22, 2014 Share Posted June 22, 2014 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. 2 Link to comment Share on other sites More sharing options...
joe_g Posted June 22, 2014 Author Share Posted June 22, 2014 thanks! didn't know about the date archiver module. Link to comment Share on other sites More sharing options...
Macrura Posted June 22, 2014 Share Posted June 22, 2014 hey joe - what about something like this https://processwire.com/talk/topic/4991-sub-arrays-dates-times-and-pure-hell/?p=48484 which starts with the events and creates the array of years from the unique values of the event years, sort of coming from the other direction i guess.. 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