PHP

Simple php annual - yearly - calendar

Simple yearly annual php calendar script


Simple php annual - yearly - calendar

Here is a simple annual yearly php calendar script. I got it from the internet ( www.gaijin.at/en/scrphpcalj.php ) , then I updated it into a function and removing the layout part from tables to bootstrap 3.  You can use the function in your controller then you echo $calendar in your view. I display the code twice once as a simple calendar and the second time I get values from my events in my database then insert it into an array then if it is there I have a a href link that click through to the event.

 

public function calendar()
{    

$month=date('n');
$year=date('Y');
$day=date('d');
$months=array('January','February','March','April','May','June','July','August','September','October','November','December');


$calendar = '<span align=center style="font-family:Verdana; font-size:18pt; color:#ff9900;">'.$year.'</span>';
$calendar .= '<div class="row">';

for ($line=1; $line<=3; $line++) {
	$calendar .= '<div class="col-md-12"><div class="row">';
	for ($column=1; $column<=4; $column++) {
		$this_month=($line-1)*4+$column;
		$first=date('w',mktime(0,0,0,$this_month,1,$year));
		$total=date('t',mktime(0,0,0,$this_month,1,$year));
		if ($first==0) $first=7;
		$calendar .= '<div class="col-md-3">';
		$calendar .= '<table border=0 align=center style="font-size:8pt; font-family:Verdana">';
		$calendar .=  '<th colspan=7 align=center style="font-size:12pt; font-family:Arial; color:#666699;">'.$months[$this_month-1].'</th>';
		$calendar .=  '<tr><td style="color:#666666"><b>Mon</b></td><td style="color:#666666"><b>Tue</b></td>';
		$calendar .=  '<td style="color:#666666"><b>Wed</b></td><td style="color:#666666"><b>Thu</b></td>';
		$calendar .=  '<td style="color:#666666"><b>Fri</b></td><td style="color:#0000cc"><b>Sat</b></td>';
		$calendar .=  '<td style="color:#cc0000"><b>Sun</b></td></tr>';
		$calendar .=  '<tr><br>';
		$i=1;
		while ($i<$first) {
			$calendar .=  '<td> </td>';
			$i++;
		}
		$i=1;
		while ($i<=$total) {
			$rest=($i+$first-1)%7;
			if (($i==$day) && ($this_month==$month)) {
				$calendar .= '<td style="font-size:8pt; font-family:Verdana; background:#ff0000;" align=center>';
			} else {
				$calendar .= '<td style="font-size:8pt; font-family:Verdana" align=center>';
			}
	if (($i==$day) && ($this_month==$month)) {      
 
       	$calendar .= '<span style="color:#ffffff;">'.$i.'</span>';

			}	else if ($rest==6) {

				$calendar .= '<span style="color:#0000cc">'.$i.'</span>';
      
      }  else if ($rest==0) {
      
          $calendar .= '<span style="color:#cc0000">'.$i.'</span>';
          
      } else {
				$calendar .= $i;
			}
   
			$calendar .= "</td>\n";
			if ($rest==0) $calendar .= "</tr>\n<tr>\n";
			$i++;
		}
		$calendar .= '</tr>';
		$calendar .= '</table>';
		$calendar .= '</div>';
	}
	$calendar .= '</div></div>';
}

$calendar .= '</div>';
$this->set('calendar' , $calendar );
$this->set('calevents' , $calevents );

}

 

The second part is where I am queried each day to see if there is an event for that day. I am sure there are much better ways to do this but I had a time constraint so did what I could in a short time.

 

 public function calenar()
    {
    
    if(is_null($this->Auth->user('id'))){
        $this->Flash->success(__('Please login to access this page.'));
        return $this->redirect(['controller' => 'users', 'action' => 'login']);
    } 
 
       
            $this->loadModel('Posts');
            $events = $this->Posts->find('all', [
            'fields' => ['Posts.slug', 'Posts.startdate', ],
            'conditions' => ['`Posts`.`contenttype_id` = 14 AND Posts.status_id = 1']                        
        ]);

        $calevents = array();
        foreach($events as $event){
         $startdate = $event->startdate;
         $newdate = $startdate->year.'-'.$startdate->month.'-'.$startdate->day;
         $calevents[$newdate] = $newdate;
        }
        
        
        //return debug($calevents);

$month=date('n');
$year=date('Y');
$day=date('d');
$months=array('January','February','March','April','May','June','July','August','September','October','November','December');


$calendar = '<span align=center style="font-family:Verdana; font-size:18pt; color:#ff9900;">'.$year.'</span>';
$calendar .= '<BR><span style="color:#0000cc;">Events are Blue</span><BR>';
$calendar .= '<div class="row">';

for ($line=1; $line<=3; $line++) {
	$calendar .= '<div class="col-md-12"><div class="row">';
	for ($column=1; $column<=4; $column++) {
		$this_month=($line-1)*4+$column;
		$first=date('w',mktime(0,0,0,$this_month,1,$year));
		$total=date('t',mktime(0,0,0,$this_month,1,$year));
		if ($first==0) $first=7;
		$calendar .= '<div class="col-md-3">';
		$calendar .= '<table border=0 align=center style="font-size:8pt; font-family:Verdana;">';
		$calendar .=  '<th colspan=7 align=center style="font-size:12pt; font-family:Arial; color:#666699;">'.$months[$this_month-1].'</th>';
		$calendar .=  '<tr><td style="color:#666666"><b>Mon</b></td><td style="color:#666666"><b>Tue</b></td>';
		$calendar .=  '<td style="color:#666666"><b>Wed</b></td><td style="color:#666666"><b>Thu</b></td>';
		$calendar .=  '<td style="color:#666666"><b>Fri</b></td><td style="color:#0000cc"><b>Sat</b></td>';
		$calendar .=  '<td style="color:#cc0000"><b>Sun</b></td></tr>';
		$calendar .=  '<tr><br>';
		$i=1;
		while ($i<$first) {
			$calendar .=  '<td> </td>';
			$i++;
		}
		$i=1;
		while ($i<=$total) {
			$rest=($i+$first-1)%7;
      
      $dayid = $year.'-'.$this_month.'-'.$i;
            
			if (($i==$day) && ($this_month==$month)) {
      
      if (in_array($dayid, $calevents)) {    
				$calendar .= '<td style="font-size:8pt; font-family:Verdana; background:#0000cc;" align=center>';
      } else {
				$calendar .= '<td style="font-size:8pt; font-family:Verdana; background:#ff0000;" align=center>';
      }
      

			} else {
      
      if (in_array($dayid, $calevents)) {    
				$calendar .= '<td style="font-size:8pt; font-family:Verdana; background:#0000cc;" align=center>';
      } else {
				$calendar .= '<td style="font-size:8pt; font-family:Verdana" align=center>';
      }
      

			}
      
			if (($i==$day) && ($this_month==$month)) {
      
      
      if (in_array($dayid, $calevents)) {    
        $calendar .= '<a href="/events?date='.$dayid.'" style="color:#fff;">'.$i.'</a>';
      } else {
       	$calendar .= '<span style="color:#fff;">'.$i.'</span>';
      }

			}	else if ($rest==6) {
      
      if (in_array($dayid, $calevents)) {    
        $calendar .= '<a href="/events?date='.$dayid.'" style="color:#fff;">'.$i.'</a>';
      } else {
				$calendar .= '<span style="color:#0000cc">'.$i.'</span>';
      }
                      

			} else if ($rest==0) {
      

      if (in_array($dayid, $calevents)) {    
        $calendar .= '<a href="/events?date='.$dayid.'" style="color:#fff;">'.$i.'</a>';
      } else {
				$calendar .= '<span style="color:#cc0000">'.$i.'</span>';
      }
      

			} else {
      
        if (in_array($dayid, $calevents)) {    
        $calendar .= '<a href="/events?date='.$dayid.'" style="color:#fff;">'.$i.'</a>';
      } else {
        $calendar .= $i;
      }

			}
   
			$calendar .= "</td>\n";
			if ($rest==0) $calendar .= "</tr>\n<tr>\n";
			$i++;
		}
		$calendar .= '</tr>';
		$calendar .= '</table>';
		$calendar .= '</div>';
	}
	$calendar .= '</div></div>';
}

$calendar .= '</div>';

$this->set('calendar' , $calendar );

}

 

Published: 21st March 2017 by

Adverts