Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Created September 29, 2011 07:31
Show Gist options
  • Select an option

  • Save mildfuzz/1250187 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/1250187 to your computer and use it in GitHub Desktop.
Calendar Class
<?php
class Mf_calendar{
private $post_type = "post";
private $prefix;
private $posts = array();
private $month;
private $year;
private $first_day;
private $month_length;
private $previous_month_length;
private $prev_month_remain;
private $last_day;
public function debug($var){
return $this->$var;
}
function __construct($post_type = 'post', $month = false, $year = false){
global $wpdb;
$pf = $wpdb->prefix;
$this->set_posttype($post_type);
$this->month = ($month ? $month : date('n'));
$this->year = ($year ? $year : date('Y'));
$this->month_length = cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year);
$this->previous_month_length = cal_days_in_month(CAL_GREGORIAN, ($this->month-1 == 0 ? 12 : $this->month-1), ($this->month-1 == 0 ? $this->year-1:$this->year));
$this->first_day = date('N',mktime(0,0,0,$this->month,1,$this->year));
$this->last_day = date('N',mktime(0,0,0,$this->month,$this->month_length,$this->year));
$this->prev_month_remain = ($this->previous_month_length-$this->first_day)+1;
}
private function set_posttype($post_type){
$this->post_type = $post_type;
$this->set_posts();
}
private function set_posts(){
//create array with post ID's and dates.
global $wpdb;
$pf = $wpdb->prefix;
$this->posts = $wpdb->get_results("SELECT ID, post_date FROM ".$pf."posts WHERE post_type='$this->post_type'");
}
private function lead_in(){
$calendar = array();
for($r=1;$r<=($this->first_day);$r++){
$row[] = ($this->prev_month_remain+$r);
}
return $row;
}
private function lead_out(){
$calendar = array();
for($r=1; $r<=7-$this->last_day;$r++){
$row[] = $r;
}
return $row;
}
private function calendar_array(){
$calendar = $this->lead_in();
for($d=1;$d<=$this->month_length;$d++){
$a['day'] = $d;
$a['has_post'] = $this->compare_dates($d,$this->month,$this->year);
$calendar[] = $a;
}
$out = $this->lead_out();
if(!$out) return $calendar;
foreach($out as $d){
$calendar[] = $d;
}
return $calendar;
}
private function compare_dates($day, $month, $year){
$date = date("Y-m-d",mktime(0,0,0,$month,$day,$year));
foreach($this->posts as $v){
$dt = explode(" ",$v->post_date);
$post_date = $dt[0];
if($date == $post_date){
return true;
}
}
}
public function create_calendar_html(){
$cal = $this->calendar_array();
$day = 0;
fb::log($cal,"day");
$echo = "<table>";
foreach($cal as $d){
$echo .= ($day == 0 ? "<tr>":"");//table row starts
$echo .= "<td class='".(is_array($d) ? "active_month" : "inactive_month")."'>";
$echo .= ($d['has_post'] ? "<a href='".$this->create_search_link($d)."'>" : "");
$echo .= (!is_array($d) ? $d : $d['day']);
$echo .= ($d['has_post'] ? "</a>":"");
$echo .= "</td>";
$echo .= ($day == 6 ? "</tr>" : "");//table row starts
$day = ($day == 6 ? 0 : $day+1);//Week loop
}
$echo .= "</table>";
return $echo;
}
private function create_search_link($day){
//function for returning search links according to search method.
return "#";
}
}
$calendar = new Mf_calendar('page');
//echo $calendar->create_calendar_html();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment