Tyssen Posted September 25, 2018 Share Posted September 25, 2018 I have three events all in October and I want my events page to list events under the month of their date field. But with the below code I get two under September and one under October. The date field values for the three events is: 2018-10-13 00:00:00 2018-10-20 00:00:00 2018-10-27 00:00:00 What am I doing wrong? function childrenInMonth($page, $month, $field = 'event_date') { $startTime = $month; $endTime = strtotime("next month", $startTime); return $page->children("$field>=$startTime, $field<$endTime, sort=$field"); } $events = ''; $start = $month = strtotime('today'); $end = strtotime('+1 year'); while($month < $end) : $event_pages = childrenInMonth($page, $month); if(count($event_pages)>0) : $events .='<h2>'. date('F', $month).'</h2> <ul>'; foreach ($event_pages as $event) : $events .= ' <li> <h3>'.$event->title.'</h3> </li> '; endforeach; $events .= '</ul>'; endif; $month = strtotime("+1 month", $month); endwhile; Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 25, 2018 Share Posted September 25, 2018 You probably need to define your $month variable more thoroughly. Currently you query everything based of of month long sections around „today“, which does not align with where month start/end. 1 Link to comment Share on other sites More sharing options...
Tyssen Posted September 25, 2018 Author Share Posted September 25, 2018 Right, changing it to $month = date('m') seems to have fixed it. Link to comment Share on other sites More sharing options...
psy Posted September 25, 2018 Share Posted September 25, 2018 This works for me... clinics (events) are listed under a Month Year heading. $clinics = $pages->find('template=event, start_date>=today, limit=6, sort=start_date'); if (!$clinics->count) return; $datetime = wire('datetime'); foreach ($clinics as $clinic) : $currentDate = $datetime->date('F Y', $clinic->start_date); if ($clinic->id == $clinics->first->id) : // first clinic?> <h3 ><?=$currentDate?></h3> <?php else : $prevDate = $datetime->date('F Y', $clinic->prev->start_date); if ( $prevDate !== $currentDate) : // different month and year ?> <h3 ><?=$currentDate?></h3> <?php endif; endif; ?> <div class="event-summary clearfix"> <?php // event details here ?> </div> <?php endforeach; ?> 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