Jump to content

Listings pages by date showing under wrong month


Tyssen
 Share

Recommended Posts

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

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

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...