Jump to content

Display results with a year heading using datetime field


cb2004
 Share

Recommended Posts

I am displaying calendar entries by using a datetime field for the start date and I will later be extending it by also displaying an end date. What I wanted to do was have the year as a heading and then all results for that year will be displayed. What I have below is actually working great, I was just wondering if it was a good technique?

$results = $page->children;

if (count($results)) {
	$years = array();

	foreach ($results as $result) {
		$years[] = strftime("%Y", $result->date_1);
	}

	$years_unique = array_unique($years);

	foreach ($years_unique as $year) {
		echo "<h3>{$year}</h3>";
		$start = strtotime(date("$year-01-01") . " 00:00:00");
		$end = strtotime(date("$year-12-31") . " 23:59:59");
		foreach ($page->children("date_1>$start, date_1<$end") as $child) {
			echo "<p>{$child->title}</p>";
		}
	}
}

Cheers all.

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