Remove passed dates in CodeIgniter Calendar - php

Is it possible to remove the dates that have already passed (in the past) in Codeigniter Calendar?
I'm using the Calendar Template that was provided in the user's manual

This is possible although I don't know if it is conventional here is my code.
Save to "/application/libraries/MY_Calendar.php"
<?php
class MY_Calendar extends CI_Calendar {
/**
* Generate the calendar
*
* #param int the year
* #param int the month
* #param array the data to be shown in the calendar cells
* #return string
*/
public function generate($year = '', $month = '', $data = array()) {
$local_time = time();
// Set and validate the supplied month/year
if (empty($year)) {
$year = date('Y', $local_time);
} elseif (strlen($year) === 1) {
$year = '200' . $year;
} elseif (strlen($year) === 2) {
$year = '20' . $year;
}
if (empty($month)) {
$month = date('m', $local_time);
} elseif (strlen($month) === 1) {
$month = '0' . $month;
}
$adjusted_date = $this->adjust_date($month, $year);
$month = $adjusted_date['month'];
$year = $adjusted_date['year'];
// Determine the total days in the month
$total_days = $this->get_total_days($month, $year);
// Set the starting day of the week
$start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
$start_day = isset($start_days[$this->start_day]) ? $start_days[$this->start_day] : 0;
// Set the starting day number
$local_date = mktime(12, 0, 0, $month, 1, $year);
$date = getdate($local_date);
$day = $start_day + 1 - $date['wday'];
while ($day > 1) {
$day -= 7;
}
// Set the current month/year/day
// We use this to determine the "today" date
$cur_year = date('Y', $local_time);
$cur_month = date('m', $local_time);
$cur_day = date('j', $local_time);
$is_current_month = ($cur_year == $year && $cur_month == $month);
// Generate the template data array
$this->parse_template();
// Begin building the calendar output
$out = $this->replacements['table_open'] . "\n\n" . $this->replacements['heading_row_start'] . "\n";
// "previous" month link
if ($this->show_next_prev === TRUE) {
// Add a trailing slash to the URL if needed
$this->next_prev_url = preg_replace('/(.+?)\/*$/', '\\1/', $this->next_prev_url);
$adjusted_date = $this->adjust_date($month - 1, $year);
$out .= str_replace('{previous_url}', $this->next_prev_url . $adjusted_date['year'] . '/' . $adjusted_date['month'], $this->replacements['heading_previous_cell']) . "\n";
}
// Heading containing the month/year
$colspan = ($this->show_next_prev === TRUE) ? 5 : 7;
$this->replacements['heading_title_cell'] = str_replace('{colspan}', $colspan,
str_replace('{heading}', $this->get_month_name($month) . ' ' . $year, $this->replacements['heading_title_cell']));
$out .= $this->replacements['heading_title_cell'] . "\n";
// "next" month link
if ($this->show_next_prev === TRUE) {
$adjusted_date = $this->adjust_date($month + 1, $year);
$out .= str_replace('{next_url}', $this->next_prev_url . $adjusted_date['year'] . '/' . $adjusted_date['month'], $this->replacements['heading_next_cell']);
}
$out .= "\n" . $this->replacements['heading_row_end'] . "\n\n"
// Write the cells containing the days of the week
. $this->replacements['week_row_start'] . "\n";
$day_names = $this->get_day_names();
for ($i = 0; $i < 7; $i++) {
$out .= str_replace('{week_day}', $day_names[($start_day + $i) % 7], $this->replacements['week_day_cell']);
}
$out .= "\n" . $this->replacements['week_row_end'] . "\n";
// Build the main body of the calendar
while ($day <= $total_days) {
$out .= "\n" . $this->replacements['cal_row_start'] . "\n";
for ($i = 0; $i < 7; $i++) {
if ($day > 0 && $day <= $total_days) {
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_start_today'] : $this->replacements['cal_cell_start'];
if ($day < date('d')) {
$out .= ' ';
} elseif (isset($data[$day])) {
// Cells with content
$temp = ($is_current_month === TRUE && $day == $cur_day) ?
$this->replacements['cal_cell_content_today'] : $this->replacements['cal_cell_content'];
$out .= str_replace(array('{content}', '{day}'), array($data[$day], $day), $temp);
} else {
// Cells with no content
$temp = ($is_current_month === TRUE && $day == $cur_day) ?
$this->replacements['cal_cell_no_content_today'] : $this->replacements['cal_cell_no_content'];
$out .= str_replace('{day}', $day, $temp);
}
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_end_today'] : $this->replacements['cal_cell_end'];
} elseif ($this->show_other_days === TRUE) {
$out .= $this->replacements['cal_cell_start_other'];
if ($day <= 0) {
// Day of previous month
$prev_month = $this->adjust_date($month - 1, $year);
$prev_month_days = $this->get_total_days($prev_month['month'], $prev_month['year']);
$out .= str_replace('{day}', $prev_month_days + $day, $this->replacements['cal_cell_other']);
} else {
// Day of next month
$out .= str_replace('{day}', $day - $total_days, $this->replacements['cal_cell_other']);
}
$out .= $this->replacements['cal_cell_end_other'];
} else {
// Blank cells
$out .= $this->replacements['cal_cell_start'] . $this->replacements['cal_cell_blank'] . $this->replacements['cal_cell_end'];
}
$day++;
}
$out .= "\n" . $this->replacements['cal_row_end'] . "\n";
}
return $out .= "\n" . $this->replacements['table_close'];
}
}
You can see my code diverges from native at line 108 with this snippet:
... if ($day < date('d')) {
$out .= ' ';
} ...
As far as pagination, future months, and past months you will need to modify the snippet as you see fit.

The answer below from William Knauss works out of the box (kudos to you William).
I actually went a little further with it and adapted William's solution to my needs.
Basically I wanted the days in the past to be disabled. As in, I wanted to put a class i the the td element for days in the past and a different class for the elements in the future (including today).
So, the code starting on line 106 looks as follows:
if ($day < date('d')) {
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_start_today'] : $this->replacements['cal_cell_start_past'];
}else{
$out .= ($is_current_month === TRUE && $day == $cur_day) ? $this->replacements['cal_cell_start_today'] : $this->replacements['cal_cell_start'];
}
if (isset($data[$day])) { // this part was restored to the way it is in the native class.
// Cells with content...
Notice the 'cal_cell_start_past'. This is a new array property in the default_template array in the native class. Starting in line 473.
Now it looks like this:
'cal_row_start' => '<tr>',
'cal_cell_start_past' => '<td class="pastDay">',
'cal_cell_start' => '<td class="day">',
'cal_cell_start_today' => '<td class="day">',
Hope this helps someone else.

Related

How to add an element for upcoming days after clicking button?

Why doesn't the following read the nextday class after clicking nextbutton?
My prev and next button code:
/prevbutton
$prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp)));
//next button
$next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));
Here is my calendar code:
$day_count = date('t', $timestamp);
// 0:Sun to 6:SAT
$str = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
$weeks = array();
$week = '';
$prevdays = true;
//Add empty cell
$week .= str_repeat('<td></td>', $str);
for ( $day = 1; $day <= $day_count; $day++, $str++)
{
$date = $ym . '-' . $day;
if ($today == $date)
{
$week .= '<td class="today">' . $day; //CSS color:green
$prevdays = false;
}
else if ($prevdays)
{
//prevdays
$week .= '<td class="prevday">'.$day; //CSS color:gray
}
else
{
//generate nextday
$week .= '<td class="nextday">'. $day; //CSS bordered green
}
//close table of weeks
$week .= '</td>';
// End of the week OR End of the month
if ($str % 7 == 6 || $day == $day_count) {
if ($day == $day_count)
{
// Add empty cell
$week .= str_repeat('<td></td>', 6 - ($str % 7));
}
$weeks[] = '<tr>' . $week . '</tr>';
// Prepare for new week
$week = '';
}
}
In the first picture, the September 2018 read code works well, but in October 2018, it show all previous days.
Here is my output:

How to add an element to past days and upcoming days (weekdays only)

i want to change the color to gray from the past weekdays
and to upcoming days change color to blue
my code is:
$week .= str_repeat('<td></td>', $str);
for ( $day = 1; $day <= $day_count; $day++, $str++)
{
$date = $ym . '-' . $day;
if ($today == $date)
{
$week .= '<td class="today">' . $day;
}
else
{
$week .= '<td>'.$day;
}
$week .= '</td>';
// End of the week OR End of the month
if ($str % 7 == 6 || $day == $day_count) {
if ($day == $day_count) {
// Add empty cell
$week .= str_repeat('<td></td>', 6 - ($str % 7));
}
$weeks[] = '<tr>' . $week . '</tr>';
// Prepare for new week
$week = '';
}
}
Here Is My Calendar
Try this:
$previous_days = true;
$week .= str_repeat('<td></td>', $str);
for ( $day = 1; $day <= $day_count; $day++, $str++)
{
$date = $ym . '-' . $day;
if ($today == $date) {
$week .= '<td class="today">' . $day;
$previous_days = false;
}else if($previous_days){
$week .= '<td class="previous-day">'.$day;
}else{
$week .= '<td class="upcoming-day">'.$day;
}
$week .= '</td>';
// End of the week OR End of the month
if ($str % 7 == 6 || $day == $day_count) {
if ($day == $day_count) {
// Add empty cell
$week .= str_repeat('<td></td>', 6 - ($str % 7));
}
$weeks[] = '<tr>' . $week . '</tr>';
// Prepare for new week
$week = '';
}
}

PHP Calendar Issues

I have the following code that I created to generate a Calendar, but it has some issues:
//Labels
$dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December");
//max values
$maxDays = 7;
$maxMonths = 12;
//stats
$forceMonth = $_GET['m'];
$forceYear = $_GET['y'];
$todayDate = date("d-m-Y");
$todayDate = date("d-m-Y", strtotime($todayDate));
$explodeToday = explode("-", $todayDate);
$currentDay = $explodeToday[0];
if(isset($forceMonth)) {
$currentMonth = $forceMonth;
} else {
$currentMonth = $explodeToday[1];
};
if(isset($forceYear)) {
$currentYear = $forceYear;
} else {
$currentYear = $explodeToday[2];
};
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
//database values
$startDate = array("01-06-2015","25-06-2015");
$endDate = array("05-06-2015","05-07-2015");
$bookedUser = array("Dexter","James");
//counters
$daysIntoMonth = 0;
$dayCounter = 0;
//debug
echo '<p>Current Month: ' .$monthLables[$currentMonth-1]. ' / ' .$currentMonth. '</p>';
echo '<p>Current Year: ' .$currentYear. '</p>';
//start of Calendar
echo '<table>';
//print days of week
echo '<tr>';
foreach($dayLabels as $day) {
echo '<td style="border-bottom:dashed 1px #DDD;">' .$day. '</td>';
};
echo '</tr>';
while($daysIntoMonth < $daysInMonth) {
//days into month
$daysIntoMonth++;
$temp_inMonth = sprintf("%02d", $daysIntoMonth);
$daysIntoMonth = $temp_inMonth;
//days into week
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
//current calendar date
$calDate = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear));
$timeCal = strtotime($calDate);
if($dayCounter == 1) {
echo '<tr>';
};
if($startKey = array_search($calDate, $startDate) !== FALSE) {
$booked = true;
};
if($endKey = array_search($calDate, $endDate) !== FALSE) {
$booked = false;
};
if($booked == true) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == true && array_search($calDate, $startDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == false && array_search($calDate, $endDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else {
echo '<td>' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
}
if($dayCounter == $maxDays) {
echo '</tr>';
$dayCounter = 0;
};
};
//table is kill
echo '</table>';
The issues I have noticed:
Unable to put a $bookedUser for respective $startDate,$endDate.
When a booking laps over to another month, it skips all the dates until the $endDate.
All Months start on Monday, how would I go about making them start of correct days of the week.
Possible code examples to help me solve my issues would be great, thanks in advance.
Edit:
I have solved problem 3 by using the following code:
$firstDayofMonth = strtotime("01-$currentMonth-$currentYear");
$firstDayofMonth = date("D", $firstDayofMonth);
$firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth = $firstDayofMonth + 1;
$startMonth = 0;
if($firstDayofMonth != 7) {
while($startMonth < $firstDayofMonth) {
echo '<td></td>';
$startMonth++;
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
};
};
For the days and months (problem 3), I would do this:
$todaysNumber = date('w');
$currentDayInText = $dayLabels[$todaysNumber];
And the same for the monhts.
Mostly, in my MySQL-tables, dates are placed like 2015-06-05 and not in European time notation. Maybe that could solve problem 1?

Display Calendar on php

I've searched around for calendars that is on PHP but all I searched are date time picker.
But what I only want is a simple calendar that shows me the date, without picking them.
I just need the calendar to display simply like how a normal calendar works in our Operating System, thanks.
Is there any way?
Here's one you can use: http://keithdevens.com/software/php_calendar
<?php
//http://keithdevens.com/software/php_calendar
$time = time();
$today = date('j', $time);
$days = array($today => array(null, null,'<div id="today">' . $today . '</div>'));
$pn = array('«' => date('n', $time) - 1, '»' => date('n', $time) + 1);
echo generate_calendar(date('Y', $time), date('n', $time), $days, 1, null, 0);
// PHP Calendar (version 2 . 3), written by Keith Devens
// http://keithdevens . com/software/php_calendar
// see example at http://keithdevens . com/weblog
// License: http://keithdevens . com/software/license
function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0, $pn = array())
{
$first_of_month = gmmktime(0, 0, 0, $month, 1, $year);
// remember that mktime will automatically correct if invalid dates are entered
// for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
// this provides a built in "rounding" feature to generate_calendar()
$day_names = array(); //generate all the day names according to the current locale
for ($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t+=86400) //January 4, 1970 was a Sunday
$day_names[$n] = ucfirst(gmstrftime('%A', $t)); //%A means full textual day name
list($month, $year, $month_name, $weekday) = explode(',', gmstrftime('%m, %Y, %B, %w', $first_of_month));
$weekday = ($weekday + 7 - $first_day) % 7; //adjust for $first_day
$title = htmlentities(ucfirst($month_name)) . $year; //note that some locales don't capitalize month and day names
//Begin calendar . Uses a real <caption> . See http://diveintomark . org/archives/2002/07/03
#list($p, $pl) = each($pn); #list($n, $nl) = each($pn); //previous and next links, if applicable
if($p) $p = '<span class="calendar-prev">' . ($pl ? '' . $p . '' : $p) . '</span> ';
if($n) $n = ' <span class="calendar-next">' . ($nl ? '' . $n . '' : $n) . '</span>';
$calendar = "<div class=\"mini_calendar\">\n<table>" . "\n" .
'<caption class="calendar-month">' . $p . ($month_href ? '' . $title . '' : $title) . $n . "</caption>\n<tr>";
if($day_name_length)
{ //if the day names should be shown ($day_name_length > 0)
//if day_name_length is >3, the full name of the day will be printed
foreach($day_names as $d)
$calendar .= '<th abbr="' . htmlentities($d) . '">' . htmlentities($day_name_length < 4 ? substr($d,0,$day_name_length) : $d) . '</th>';
$calendar .= "</tr>\n<tr>";
}
if($weekday > 0)
{
for ($i = 0; $i < $weekday; $i++)
{
$calendar .= '<td> </td>'; //initial 'empty' days
}
}
for($day = 1, $days_in_month = gmdate('t',$first_of_month); $day <= $days_in_month; $day++, $weekday++)
{
if($weekday == 7)
{
$weekday = 0; //start a new week
$calendar .= "</tr>\n<tr>";
}
if(isset($days[$day]) and is_array($days[$day]))
{
#list($link, $classes, $content) = $days[$day];
if(is_null($content)) $content = $day;
$calendar .= '<td' . ($classes ? ' class="' . htmlspecialchars($classes) . '">' : '>') .
($link ? '' . $content . '' : $content) . '</td>';
}
else $calendar .= "<td>$day</td>";
}
if($weekday != 7) $calendar .= '<td id="emptydays" colspan="' . (7-$weekday) . '"> </td>'; //remaining "empty" days
return $calendar . "</tr>\n</table>\n</div>\n";
}
?>
It's a pretty basic mini calendar. No fancy functions.
I have written just one such with PHP5 + ajax(if you are need to use it):
<?php
class PN_Calendar {
public $first_day_of_week = 0; //0 - sunday, 1 - monday
public $current_year = null;
public $current_month = null;
public $current_day = null;
public $show_year_selector = true;
public $min_select_year = 1991;
public $max_select_year = 2045;
public $show_month_selector = true;
public function __construct($atts = array()) {
if (isset($atts['first_day_of_week'])) {
$this->first_day_of_week = $atts['first_day_of_week'];
}
if (!isset($atts['year'])) {
$this->current_year = date('Y');
} else {
$this->current_year = $atts['year'];
}
if (!isset($atts['month'])) {
$this->current_month = date('m');
} else {
$this->current_month = $atts['month'];
}
if (!isset($atts['day'])) {
$this->current_day = date('d');
} else {
$this->current_day = $atts['day'];
}
//***
if (isset($atts['show_year_selector'])) {
$this->show_year_selector = $atts['show_year_selector'];
}
if (isset($atts['show_month_selector'])) {
$this->show_month_selector = $atts['show_month_selector'];
}
if (isset($atts['min_select_year'])) {
$this->min_select_year = $atts['min_select_year'];
}
if (isset($atts['max_select_year'])) {
$this->max_select_year = $atts['max_select_year'];
}
}
/*
* Month calendar drawing
*/
public function draw($data = array(), $y = 0, $m = 0) {
//***
if ($m == 0 AND $m == 0) {
$y = $this->current_year;
$m = $this->current_month;
}
//***
$data['week_days_names'] = $this->get_week_days_names(true);
$data['cells'] = $this->generate_calendar_cells($y, $m);
$data['month_name'] = $this->get_month_name($m);
$data['year'] = $y;
$data['month'] = $m;
return $this->draw_html('calendar', $data);
}
private function generate_calendar_cells($y, $m) {
$y = intval($y);
$m = intval($m);
//***
$first_week_day_in_month = date('w', mktime(0, 0, 0, $m, 1, $y)); //from 0 (sunday) to 6 (saturday)
$days_count = $this->get_days_count_in_month($y, $m);
$cells = array();
//***
if ($this->first_day_of_week == $first_week_day_in_month) {
for ($d = 1; $d <= $days_count; $d++) {
$cells[] = new PN_CalendarCell($y, $m, $d);
}
//***
$cal_cells_left = 5 * 7 - $days_count;
$next_month_data = $this->get_next_month($y, $m);
for ($d = 1; $d <= $cal_cells_left; $d++) {
$cells[] = new PN_CalendarCell($next_month_data['y'], $next_month_data['m'], $d, false);
}
} else {
//***
if ($this->first_day_of_week == 0) {
$cal_cells_prev = 6 - (7 - $first_week_day_in_month); //checked, is right
} else {
if ($first_week_day_in_month == 1) {
$cal_cells_prev = 0;
} else {
if ($first_week_day_in_month == 0) {
$cal_cells_prev = 6 - 1;
} else {
$cal_cells_prev = 6 - (7 - $first_week_day_in_month) - 1;
}
}
}
//***
$prev_month_data = $this->get_prev_month($y, $m);
$prev_month_days_count = $this->get_days_count_in_month($prev_month_data['y'], $prev_month_data['m']);
for ($d = $prev_month_days_count - $cal_cells_prev; $d <= $prev_month_days_count; $d++) {
$cells[] = new PN_CalendarCell($prev_month_data['y'], $prev_month_data['m'], $d, false);
}
//***
for ($d = 1; $d <= $days_count; $d++) {
$cells[] = new PN_CalendarCell($y, $m, $d);
}
//***
//35(7*5) or 42(7*6) cells
$busy_cells = $cal_cells_prev + $days_count;
$cal_cells_left = 0;
if ($busy_cells < 35) {
$cal_cells_left = 35 - $busy_cells - 1;
} else {
$cal_cells_left = 42 - $busy_cells - 1;
}
//***
if ($cal_cells_left > 0) {
$next_month_data = $this->get_next_month($y, $m);
for ($d = 1; $d <= $cal_cells_left; $d++) {
$cells[] = new PN_CalendarCell($next_month_data['y'], $next_month_data['m'], $d, false);
}
}
}
//***
return $cells;
}
public function get_next_month($y, $m) {
$y = intval($y);
$m = intval($m);
//***
$m++;
if ($m % 13 == 0 OR $m > 12) {
$y++;
$m = 1;
}
return array('y' => $y, 'm' => $m);
}
public function get_prev_month($y, $m) {
$y = intval($y);
$m = intval($m);
//***
$m--;
if ($m <= 0) {
$y--;
$m = 12;
}
return array('y' => $y, 'm' => $m);
}
public function get_days_count_in_month($year, $month) {
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
public static function get_month_name($m) {
$names = self::get_monthes_names();
return $names[intval($m)];
}
public function get_week_day_name($num, $shortly = false) {
$names = $this->get_week_days_names($shortly);
return $names[intval($num)];
}
public function get_week_days_names($shortly = false) {
if ($this->first_day_of_week == 1) {
if ($shortly) {
return array(
1 => 'Mo',
2 => 'Tu',
3 => 'We',
4 => 'Th',
5 => 'Fr',
6 => 'Sa',
7 => 'Su'
);
}
return array(
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
);
} else {
if ($shortly) {
return array(
0 => 'Su',
1 => 'Mo',
2 => 'Tu',
3 => 'We',
4 => 'Th',
5 => 'Fr',
6 => 'Sa'
);
}
return array(
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday'
);
}
}
public static function get_monthes_names() {
return array(
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
);
}
public function draw_html($view, $data = array()) {
#extract($data);
ob_start();
include('views/' . $view . '.php' );
return ob_get_clean();
}
}
class PN_CalendarCell {
public $cell_year = null;
public $cell_month = null;
public $cell_day = null;
public $in_current_month = true;
public function __construct($y, $m, $d, $in_current_month = true) {
$this->cell_year = $y;
$this->cell_month = $m;
$this->cell_day = $d;
$this->in_current_month = $in_current_month;
}
public function get_week_day_num() {
return date('w', mktime(0, 0, 0, $this->cell_month, $this->cell_day, $this->cell_year)); //from 0 (sunday) to 6 (saturday);
}
public function draw($events) {
$this_day_events = 0;
if (is_array($events)) {
if (isset($events[$this->cell_year][$this->cell_month][$this->cell_day])) {
$this_day_events = count($events[$this->cell_year][$this->cell_month][$this->cell_day]);
}
} else {
$events = array();
}
?>
<span class="pn_cal_cell_ev_counter" <?php if ($this_day_events <= 0): ?>style="display: none;"<?php endif; ?>><?php echo $this_day_events ?></span>
<a data-year="<?php echo $this->cell_year ?>" data-month="<?php echo $this->cell_month ?>" data-day="<?php echo $this->cell_day ?>" data-week-day-num="<?php echo $this->get_week_day_num() ?>" href="javascript:void(0);" class="<?php if ($this->in_current_month): ?>pn_this_month<?php else: ?>other_month<?php endif; ?>"><?php echo $this->cell_day ?></a>
}
}
Script calling:
<?php
include_once 'classes/calendar.php';
$calendar = new PN_Calendar();
echo $calendar->draw();
?>
Download script with demo: http://pluginus.net/archives/simple-free-open-source-ajax-php-calendar-script
Demo: http://demo.pluginus.net/calendar/
I also was looking for a simple solution, a PHP class that rendered me a monthly calendar where I could add easily events.
I've found this PHP Calendar http://coreyworrell.com/calendar/
You have to still create a view/template in html, but you can find a CSS and HTML version there that you can use.
And it has custom events also in PHP
$event = $calendar->event()
->condition('timestamp', strtotime('January 2, 2010'))
->title('Hello All')
->output('My Custom Event')
->add_class('custom-event-class');

Generate a 2 week view with Codeigniter's Calendar library from current week

I'm trying to make a 2 week view with Codeigniter's calendar library, basically I don't need a full month view, instead I'd like to have a 2 week view from the current week.
The image above shows similarity what I want to achieve. On the templating side of Codeigniter when generating does not offer this kind of feature. I do not want to use jQuery UI's calendar due to I need a static calendar instead of relying on JS to perform this (especially when users have disabled JS).
Is there a specific extended library I can merge with Codeigniter's or a specific template string that can perform this type of calendar view?
You have to extend the calendar library for this.
In application/libraries create a MY_Calendar.php:
class MY_Calendar extends CI_Calendar {
public function generate2weeks($year = '', $month = '', $data = array())
{
// code goes here
}
}
Have look at the CI_Calendar::generate() function. You have to write a new function generate2weeks() where you basically do the same as in generate() but with less days. I would make a copy of generate() and work from here. Maybe it would be enough to overwrite get_total_days worth a try.
You need to extend the Calendar class and override the generate() method. I've spent a little bit of time to tweak the generate() function and this is what I got.
function generate($year = '', $month = '', $data = array())
{
// Set and validate the supplied month/year
if ($year == '')
$year = date("Y", $this->local_time);
if ($month == '')
$month = date("m", $this->local_time);
if (strlen($year) == 1)
$year = '200'.$year;
if (strlen($year) == 2)
$year = '20'.$year;
if (strlen($month) == 1)
$month = '0'.$month;
$adjusted_date = $this->adjust_date($month, $year);
$month = $adjusted_date['month'];
$year = $adjusted_date['year'];
// Determine the total days in the month
$total_days = $this->get_total_days($month, $year);
// Set the starting day of the week
$start_days = array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
$start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];
// Set the starting day number
$local_date = mktime(12, 0, 0, $month, date('j'), $year);
$date = getdate($local_date);
$day = $date["mday"];
// Set the current month/year/day
// We use this to determine the "today" date
$cur_year = date("Y", $this->local_time);
$cur_month = date("m", $this->local_time);
$cur_day = date("j", $this->local_time);
$is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;
// Generate the template data array
$this->parse_template();
// Begin building the calendar output
$out = $this->temp['table_open'];
$out .= "\n";
$out .= "\n";
$out .= $this->temp['heading_row_start'];
$out .= "\n";
// "previous" month link
if ($this->show_next_prev == TRUE)
{
// Add a trailing slash to the URL if needed
$this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->next_prev_url);
$adjusted_date = $this->adjust_date($month - 1, $year);
$out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
$out .= "\n";
}
// Heading containing the month/year
$colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
$this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
$this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)." ".$year, $this->temp['heading_title_cell']);
$out .= $this->temp['heading_title_cell'];
$out .= "\n";
// "next" month link
if ($this->show_next_prev == TRUE)
{
$adjusted_date = $this->adjust_date($month + 1, $year);
$out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
}
$out .= "\n";
$out .= $this->temp['heading_row_end'];
$out .= "\n";
// Write the cells containing the days of the week
$out .= "\n";
$out .= $this->temp['week_row_start'];
$out .= "\n";
$day_names = $this->get_day_names();
for ($i = 0; $i < 7; $i ++)
{
$out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
}
$out .= "\n";
$out .= $this->temp['week_row_end'];
$out .= "\n";
// Build the main body of the calendar
$limit = $day + 13;
if ($limit > $total_days)
{
$total_days_left = $limit - $total_days;
}
while ($day <= $limit)
{
$out .= "\n";
$out .= $this->temp['cal_row_start'];
$out .= "\n";
for ($i = 0; $i < 7; $i++)
{
if ($day > $total_days)
{
$day = 1;
$limit = $total_days_left;
}
$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
if ($day > 0 AND $day <= $limit)
{
if (isset($data[$day]))
{
// Cells with content
$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_content_today'] : $this->temp['cal_cell_content'];
$out .= str_replace('{day}', $day, str_replace('{content}', $data[$day], $temp));
}
else
{
// Cells with no content
$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
$out .= str_replace('{day}', $day, $temp);
}
}
else
{
// Blank cells
$out .= $this->temp['cal_cell_blank'];
}
$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];
$day++;
}
$out .= "\n";
$out .= $this->temp['cal_row_end'];
$out .= "\n";
}
$out .= "\n";
$out .= $this->temp['table_close'];
return $out;
}
You can see the differences between this one and the original one here:
http://www.diffnow.com/?report=51mkw
This will shows 2 weeks from the current date. But when you call the calendar library you need to specify the start_day to the current day by using date('l') and strtolower() like:
$prefs = array (
'start_day' => strtolower(date('l')),
);
$this->load->library('calendar', $prefs);
Otherwise, the days would show up incorrectly.

Categories