daylength parameter, also accepts string parameters for three symbols to go after the next month link, before the previous month link, and between the two to represent "go to blog home." The month-and-year at the top of the calendar is a link to the current month's posts. This version of the calendar also adds class="posts" to the cell of any date containing posts, and class values of first or last to the first or last cells in a given table row. Finally, the calendar does not generate a tfoot at all if there are no previous or next months (unlikely, but it happens). Version: 1.1 Date: 1 February 2005 Author: Eric A. Meyer Author URI: http://meyerweb.com/ */ function mw_get_calendar($daylength = 1, $nextSymbol='', $prevSymbol='', $currentSymbol='') { global $wpdb, $HTTP_GET_VARS, $m, $monthnum, $year, $timedifference, $month, $weekday, $tableposts, $posts; // Quick check. If we have no posts at all, abort! if (!$posts) { $gotsome = $wpdb->get_var("SELECT ID from $tableposts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); if (!$gotsome) return; } if (isset($HTTP_GET_VARS['w'])) { $w = ''.intval($HTTP_GET_VARS['w']); } $time_difference = get_settings('time_difference'); // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = ''.intval($monthnum); $thisyear = ''.intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = ''.intval(substr($m, 0, 4)); $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')"); } elseif (!empty($m)) { $calendar = substr($m, 0, 6); $thisyear = ''.intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = ''.intval(substr($m, 4, 2)); } } else { $thisyear = intval(date('Y', time()+($time_difference * 3600))); $thismonth = intval(date('m', time()+($time_difference * 3600))); } $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); // Get the next and previous month and year with at least one post $previous = $wpdb->get_row("SELECT DISTINCT MONTH( post_date ) AS month, YEAR( post_date ) AS year FROM $tableposts WHERE post_date < '$thisyear-$thismonth-01' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1"); $next = $wpdb->get_row("SELECT DISTINCT MONTH( post_date ) AS month, YEAR( post_date ) AS year FROM $tableposts WHERE post_date > '$thisyear-$thismonth-01' AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' ) AND post_status = 'publish' ORDER BY post_date ASC LIMIT 1"); echo ''; $i = 0; foreach ($weekday as $wd) { $cl = ''; if ($i == 0) {$cl = ' class="first"';} if ($i == 6) {$cl = ' class="last"';} echo ''; $i += 1; } echo ' '; if ($previous) { echo "\n\t\t".''; } else { echo "\n\t\t".''; } echo "\n\t\t".''; if ($next) { echo "\n\t\t".''; } else { echo "\n\t\t".''; } echo ' '; // Get days with posts $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) FROM $tableposts WHERE MONTH(post_date) = $thismonth AND YEAR(post_date) = $thisyear AND post_status = 'publish' AND post_date < '" . date("Y-m-d H:i:s", (time() + ($time_difference * 3600)))."'", ARRAY_N); if ($dayswithposts) { foreach ($dayswithposts as $daywith) { $daywithpost[] = $daywith[0]; } } else { $daywithpost = array(); } if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") || strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "camino") || strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), "safari")) { $ak_title_separator = "\n"; } else { $ak_title_separator = ", "; } $ak_titles_for_day = array(); $ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom " ."FROM $tableposts " ."WHERE YEAR(post_date) = '$thisyear' " ."AND MONTH(post_date) = '$thismonth' " ."AND post_date < '".date("Y-m-d H:i:s", (time() + ($time_difference * 3600)))."' " ."AND post_status = 'publish'" ); if ($ak_post_titles) { foreach ($ak_post_titles as $ak_post_title) { if (empty($ak_titles_for_day["day_".$ak_post_title->dom])) { $ak_titles_for_day["day_".$ak_post_title->dom] = ''; } if (empty($ak_titles_for_day["$ak_post_title->dom"])) { // first one $ak_titles_for_day["$ak_post_title->dom"] .= htmlspecialchars(stripslashes($ak_post_title->post_title)); } else { $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . htmlspecialchars(stripslashes($ak_post_title->post_title)); } } } // See how much we should pad in the beginning $pad = intval(date('w', $unixmonth)); if (0 != $pad) echo "\n\t\t"; $daysinmonth = intval(date('t', $unixmonth)); for ($day = 1; $day <= $daysinmonth; ++$day) { $cl = ''; if (isset($newrow) && $newrow) echo "\n\t\n\t\n\t\t"; $newrow = false; echo ''; } $pad = 7 - date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear)); if ($pad != 0 && $pad != 7) echo "\n\t\t"; echo "\n\t\n\t\n\t
' . $month[zeroise($thismonth, 2)] . ' ' . $thisyear . '
' . substr($wd, 0, $daylength) . '
'.$prevSymbol.' ' . $month[zeroise($previous->month, 2)] . ' '.$currentSymbol.'' . $month[zeroise($next->month, 2)] . ' '.$nextSymbol.' 
 
$day"; } else { if ($cl != '') { echo 'class="' . $cl . '">' . $day; } else { echo '>' . $day; } } echo ' 
"; } ?>