<?php
class MonthCalendar
{
public $locale = ''; # show https://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.90).aspx
# show https://msdn.microsoft.com/en-us/library/cdax410z(v=vs.90).aspx
# Linux: show results from `locale -a`
# Linux: show /etc/locale.gen, edit and use `sudo locale-gen`
public $maxDayNamesLength = 2; # 1 | 2 | 3 | 99
public $datum = false;
public $htmlCalendarId = 'Calendar';
public $htmlCalendarClass = 'Calendar';
public $htmlThisDayId = 'ThisDay';
public $htmlHolidayClass = 'Holyday';
public $htmlPartialHolidayClass = 'PartialHolyday';
public $htmlSondayClass = 'Son';
public $htmlSaturdayClass = 'Sat';
public $yearNextSymbol = '⇨';
public $yearBeforeSymbol = '⇦';
public $monthNextSymbol = '→';
public $monthBeforeSymbol = '←';
public $arMonthList;
public $arWeekdayList;
public $daysSprintf = '%02d';
public $daysLinkSprintf = 'calendar.php?y=%04d&m=%02d&d=%02d';
public $outsideDaysShow = ' ';
public $showYear = true;
public $showMonth = true;
public $showWeekNr = true;
public function __construct() {
$this -> datum = mktime( 0,0,0, date('n'), 1, date('Y') );
if ($this -> locale) {
$this -> setLocale ( $this -> locale );
}
}
private function getMonthNames() {
for ( $i=1; $i<13; $i++ ) {
$d = mkTime( 0, 0, 0, $i, 1, 1971 );
$names[$i] = strftime( '%B', $d );
}
$names[0] = false;
return $names;
}
private function getWeakDayNames() {
for ( $i = 1; $i < 8; $i++ ) {
$d = mkTime(0, 0, 0, 1, 3+$i, 1971);
$names[$i] = mb_substr( strftime( '%A', $d ) , 0, $this -> maxDayNamesLength);
}
$names[0] = $names[7];
return $names;
}
public function setLocale( $locale ) {
if (false === setlocale( LC_TIME, $locale ) ) {
trigger_error("Ungültige Locale-Einstellung: $locale", E_USER_NOTICE);
}
$this -> arMonthList = $this -> getMonthNames();
$this -> arWeekdayList = $this -> getWeakDayNames();
}
public function setYear($y) {
$i = intval($y);
$t = mktime(1, 1, 1, 12, 31, $y);
if ( $t && $i == $y ) {
$m = date('n', $this -> datum );
$this -> datum = mktime( 0, 0, 0, $m, 1, $y );
return true;
} else {
trigger_error("Ungültiges Jahr: $y" );
return false;
}
}
public function setMonth($m) {
$i = intval($m);
if ($m > 0 && $m <= 12 && $i == $m ) {
$y = date('y', $this -> datum );
if ( mktime( 0,0,0, $m, 1, $y ) ) {
$this -> datum = mktime( 0,0,0, $m, 1, $y );
return true;
} else {
trigger_error("UngültigesDatum");
}
} else {
return false;
}
}
public function setYearNext () {
$y = date( 'Y', $this -> datum ) + 1;
$m = date( 'n', $this -> datum );
if ( mktime( 0,0,0, $m, 1, $y ) ) {
$this -> datum = mktime( 0,0,0, $m, 1, $y );
return true;
} else {
trigger_error("Ungültiges Jahr: " . $y );
return false;
}
}
public function setYearBefore () {
$y = date( 'Y', $this -> datum ) - 1;
$m = date( 'n', $this -> datum );
if ( mktime( 0,0,0, $m, 1, $y ) ) {
$this -> datum = mktime( 0,0,0, $m, 1, $y );
return true;
} else {
trigger_error("Ungültiges Jahr: " . $y );
return false;
}
}
public function setMonthNext () {
$y = date( 'Y', $this -> datum );
$m = date( 'n', $this -> datum ) + 1;
if ( mktime( 0,0,0, $m, 1, $y ) ) {
$this -> datum = mktime( 0,0,0, $m, 1, $y );
return true;
} else {
trigger_error("UngültigesDatum");
return false;
}
}
public function setMonthBefore () {
$y = date( 'Y', $this -> datum );
$m = date( 'n', $this -> datum ) - 1;
if ( mktime( 0,0,0, $m, 1, $y ) ) {
$this -> datum = mktime( 0,0 ,0, $m, 1, $y );
return true;
} else {
trigger_error("UngültigesDatum");
return false;
}
}
public function getCalendar() {
if ( $this -> htmlCalendarId ) {
$htmlCalendarId = ' id="' . $this -> htmlCalendarId .'"';
}
if ( $this -> htmlCalendarClass ) {
$htmlCalendarClass=' class="' . $this -> htmlCalendarClass .'"';
}
if ( $this -> showWeekNr ) {
$headerCols=8;
} else {
$headerCols=6;
}
$year = date( 'Y', $this -> datum );
$month = date( 'n', $this -> datum );
$out = '
<table' . $htmlCalendarId . $htmlCalendarClass . '>
<thead>';
if ( $this -> showYear ) {
$out .= '
<tr class="CalendarYearRow">
<th>' . $this -> yearBeforeSymbol . '</th>
<th colspan=' . ( $headerCols - 2 ) . '>' . $year . '</th>
<th>' . $this -> yearNextSymbol . '</th>
</tr>';
}
if ( $this -> showMonth ) {
$out .= '
<tr class="CalendarMountRow">
<th>' . $this -> monthBeforeSymbol . '</th>
<th colspan=' . ($headerCols - 2) . '>' . $this -> arMonthList[$month] . '</th>
<th>' . $this -> monthNextSymbol . '</th>
</tr>';
}
$out .= '
<tr class="CalendarWeekDayRow">';
if ( $this -> showWeekNr ) {
$out .= '
<th class="weeksColumn"></th>';
}
$out .= '
<th>' . $this -> arWeekdayList[1] . '</th>
<th>' . $this -> arWeekdayList[2] . '</th>
<th>' . $this -> arWeekdayList[3] . '</th>
<th>' . $this -> arWeekdayList[4] . '</th>
<th>' . $this -> arWeekdayList[5] . '</th>
<th>' . $this -> arWeekdayList[6] . '</th>
<th>' . $this -> arWeekdayList[7] . '</th>
</tr>';
$firstWeekDay = date( 'N', $this -> datum );
$WeekNumber = date( 'W', $this -> datum );
$lastDayofMounth = date( 't', $this -> datum );
# Nicht zugehörige Tage:
$out .= '
<tr>';
if ( $this -> showWeekNr ) {
$out .= '
<th class="weeksColumn">' . sprintf($this -> daysSprintf, $WeekNumber) . '</th>';
}
$counter = 0;
for ($i = 1; $i<$firstWeekDay; $i++) {
$out .= '
<td class="outsideDay">' . $this -> outsideDaysShow . '</td>';
$counter++;
}
# zugehörige Tage:
for ($i=1; $i <= $lastDayofMounth; $i++) {
if ( ! $this -> daysLinkSprintf ) {
$link='';
$endlink='';
} else {
$link = '<a href="' . sprintf ($this -> daysLinkSprintf, date( 'Y', $this -> datum ), date( 'n', $this -> datum ), $i ) . '">';
$endlink = '</a>';
}
if ( 6 == ( $counter + 8 ) % 7 ) {
$class = 'class="' . $this -> htmlSaturdayClass .'" ';
} else if ( 0 == ( $counter + 8 ) % 7 ) {
$class = 'class="' . $this -> htmlSondayClass .'" ';
} else if ( date('Y') == $year && date('n') == $month && date('j') == $i ) {
$class = 'class="today" ';
} else {
$class = '';
}
$out .= '
<td ' . $class . 'data-Datum="' . sprintf( '%04d-%02d-%02d', date( 'Y', $this->datum ), date( 'm', $this->datum ), $i ) . '">' . $link . sprintf( $this -> daysSprintf, $i ) . $endlink . '</td>';
$counter++;
if ($i < $lastDayofMounth && 0 == ( $counter ) % 7 ) {
$out .= '
</tr>
<tr>';
if ( $this -> showWeekNr ) {
if ( 53 == $WeekNumber) {
$WeekNumber = 0;
}
$out .= '
<th class="weeksColumn">' . sprintf($this -> daysSprintf, ++$WeekNumber) . '</th>';
}
}
}
# Nicht zugehörige Tage:
while (0 != $counter % 7) {
$out .= '
<td class="outsideDay">' . $this -> outsideDaysShow . '</td>';
$counter++;
}
$out .= '
</tr>
</tbody>
</table>
';
return $out;
}
public function printCalendar() {
echo $this -> getCalendar(), "\n";
}
}
Aufruf:
<?php
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
require 'MonthCalendar.php';
$o = new MonthCalendar();
#$o -> setLocale( 'de_DE.utf-8' );
$o -> setLocale( 'de_AT.utf-8' );
#$o -> setLocale( 'fi_FI.utf-8' );
#$o -> setLocale( 'ar_YE.utf-8' );
if ( isset( $_GET['action'] ) ) {
if( 'nextYear' == $_GET['action'] ) { $o-> setYearNext(); }
if( 'earlierYear' == $_GET['action'] ) { $o-> setYearBefore(); }
if( 'nextMonth' == $_GET['action'] ) { $o-> setMonthNext(); }
if( 'earlierMonth' == $_GET['action'] ) { $o-> setMonthBefore(); }
}
if ( isset( $_GET['y'] ) ) { $o-> setYear($_GET['y']); }
if ( isset( $_GET['m'] ) ) { $o-> setMonth($_GET['m']); }
$o->daysSprintf = '%d'; # '%02d'-> "01" (default) , '%d' -> "1"
#$o -> setYear(2200);
#$o -> setMonthNext();
$o -> daysLinkSprintf='https://home.fastix.org/Tests/PHP:Feiertage/test-feiertage-form.php?jahr=%04d&monat=%02d&tag=%02d';
?>
<!Doctype html>
<html>
<head>
<title>Kalender</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<style type="text/css">
body, html {
font-family: sans-serif;
}
#Calendar td,
#Calendar th {
text-align:center;
min-width:2.0em;
border: 2px solid transparent;
border-collapse: collapse;
border-radius: 5px;
}
#Calendar td.today {
background-color: #9c27b0;
color:#f5f5f5;
font-weight:900;
}
#Calendar td a,
#Calendar td a:link,
#Calendar td a:visited,
#Calendar td a:hover,
#Calendar td a:focus {
display:block;
width:100%;
height:100%;
text-decoration: none;
color: inherit;
}
#Calendar th {
background-color: #b2ebf2;
}
#Calendar td.Sat {
background-color: #f8bbd0;
}
#Calendar td.Son {
background-color: #f48fb1;
}
#Calendar td:hover {
border-color: #d1c4e9;
font-weight: 900;
}
#Calendar td.outsideDay:hover {
border-color: transparent;
}
</style>
<head>
<body>
<hr>
<?php
for ( $m=1; $m < 13; $m++ ) {
#$o = new MonthCalendar();
$o -> setMonth($m);
echo $o -> getCalendar(), "<br>\n";
}
?>
</body>
</html>