Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Display a heat calendar for a month of data.
<?php
// Create an array of timestamps for posts
// and put them into an array called $timestamps.
foreach ($timestamps as $timestamp) {
$day = date("j", $timestamp);
$hour = date("G", $timestamp);
if (isset($heatcalendar['posts'][$day][$hour])) {
$heatcalendar['posts'][$day][$hour]++;
} else {
$heatcalendar['posts'][$day][$hour] = 1;
}
}
$heatcalendar['days'] = date("t", $timestamps[0]);
$heatcalendar['month'] = date("F", $timestamps[0]);
$heatcalendar['hours'] = 4; // How many hours in a "slice" of a 24 hour day
echo "<div style=\"max-width: 100%; overflow: auto\">\n";
echo "<table class=\"meta\" style=\"width: 100%;\" summary=\"Posts by time of day\">\n";
echo "<thead>\n";
echo "<tr>\n";
echo "<td></td>\n";
for ($day = 1; $day <= $heatcalendar['days']; $day = $day + 1) {
echo "<th scope=\"col\" id=\"day".$day."\" aria-label=\"".$heatcalendar['month']." ".$day."\">";
if ($day % 5 == 0) {
echo $day."th";
} else {
echo "&nbsp;&nbsp;&nbsp;&nbsp;";
}
echo "</th>\n";
}
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
for ($hour = 0; $hour < 24; $hour = $hour + $heatcalendar['hours']) {
echo "<tr>\n";
echo "<th scope=\"row\" id=\"hour".$hour."\">";
echo date("ga", strtotime($hour.":00"));
echo "</th>\n";
for ($day = 1; $day <= $heatcalendar['days']; $day = $day + 1) {
$total = 0;
for ($i = 0; $i < $heatcalendar['hours']; $i = $i + 1) {
if (isset($heatcalendar['posts'][$day][$hour + $i])) {
$total = $total + $heatcalendar['posts'][$day][$hour + $i];
}
}
if ($total > 0) {
echo "<td title=\"";
echo $total;
echo " post";
if ($total != 1) {
echo "s";
}
echo "\" headers=\"day".$day." hour".$hour."\">";
echo "<span style=\"all: initial; border-radius: 50%; width: 1em; height: 1em; background-color: currentcolor; display: inline-block; transform: scale(";
echo 0.5 + (0.1 * $total);
echo "); opacity: ";
echo 0.1 + (0.1 * $total);
echo "\" aria-describedby=\"day".$day." hour".$hour."\" aria-label=\"";
echo $total;
echo " post";
if ($total != 1) {
echo "s";
}
echo "\">";
echo "&nbsp;";
echo "</span>";
} else {
echo "<td>";
}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</tbody>\n";
echo "</table>\n";
echo "</div>\n";
echo "<br />\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment