This article describes a simple way to create a calendar in PHP. Share to everyone for your reference, specific as follows:
<?php function Build_calendar ($month, $year) {//Create array containing abbreviations of days of week.
$daysOfWeek = Array (' s ', ' M ', ' t ', ' W ', ' t ', ' F ', ' s ');
What is the ' the ' month in question?
$firstDayOfMonth = Mktime (0,0,0, $month, 1, $year);
How many days does this month contain?
$numberDays = date (' t ', $firstDayOfMonth);
Retrieve some information about the ' the '//month in question.
$dateComponents = getdate ($firstDayOfMonth);
What is the name of the month in question?
$monthName = $dateComponents [' Month '];
What is the index value (0-6) of the The The//month in question.
$dayOfWeek = $dateComponents [' wday '];
Create the table tag opener and day headers $calendar = "<table class= ' Calendar ' >";
$calendar. = "<caption> $monthName $year </caption>";
$calendar. = "<tr>"; Create the Calendar headers foreach ($daysOfWeek as $day) {$calendar. = "<th class= ' header ' > $daY</th> ";
//Create The rest of the calendar//Initiate the day counter, starting with the 1st.
$currentDay = 1;
$calendar. = "</tr><tr>";
The variable $dayOfWeek is used to//ensure that calendar//display consists of exactly 7 columns.
if ($dayOfWeek > 0) {$calendar. = "<td colspan= ' $dayOfWeek ' > </td>";
$month = Str_pad ($month, 2, "0", str_pad_left); while ($currentDay <= $numberDays) {//Seventh column (Saturday) reached.
Start a new row.
if ($dayOfWeek = = 7) {$dayOfWeek = 0;
$calendar. = "</tr><tr>";
$currentDayRel = Str_pad ($currentDay, 2, "0", str_pad_left);
$date = "$year-$month-$currentDayRel";
$calendar. = "<td class= ' Day ' rel= ' $date ' > $currentDay </td>";
Increment counters $currentDay + +;
$dayOfWeek + +; }//Complete The row of the last week in month, if necessary if ($dayOfWeek!= 7) {$remainingDAys = 7-$dayOfWeek;
$calendar. = "<td colspan= ' $remainingDays ' > </td>";
$calendar. = "</tr>";
$calendar. = "</table>";
return $calendar;
//Call method Echo Build_calendar (05,2016);?>
The results of the operation are shown in the following illustration:
About the online display date can also refer to the site online tools:
Online Perpetual calendar
Web Calendar
Online Perpetual Calendar Almanac Flash Edition
For more information about PHP interested readers can view the site topics: "PHP date and Time usage summary", "PHP Mathematical Calculation Skills Summary", "PHP Array" operation Skills Encyclopedia, "PHP object-oriented Program Design Introductory Course", "PHP string" Usage Summary, "PHP+MYSQL database operation Getting Started" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.