- function Build_calendar ($month, $year, $dateArray) {
- Calendar header, beginning in Sunday until Saturday
- $daysOfWeek = Array (' s ', ' M ', ' t ', ' W ', ' t ', ' F ', ' s ');
- Location of the first day of the month
- $firstDayOfMonth = Mktime (0,0,0, $month, 1, $year);
- Get the number of days this month
- $numberDays = date (' t ', $firstDayOfMonth);
- Get the first day of the month
- $dateComponents = getdate ($firstDayOfMonth);
- Get the English word of the month
- $monthName = $dateComponents [' Month '];
- $dayOfWeek = $dateComponents [' wday '];
- Month table Header
- $calendar = "
- $calendar. = "
- $calendar. = "
- }
- Use variable $dayOfWeek to ensure accurate output seven days a week
- }
- }
- }
- $calendar. = "
- Week table Header
- foreach ($daysOfWeek as $day) {
- $calendar. = "
- Start Output Calendar
- Initialize the number of days counter, starting with number 1th
- $currentDay = 1;
- $calendar. = "
- if ($dayOfWeek > 0) {
- $calendar. = "
- $month = Str_pad ($month, 2, "0", str_pad_left);
- while ($currentDay <= $numberDays) {
- 7 day line, 7 days one to add a row
- if ($dayOfWeek = = 7) {
- $dayOfWeek = 0;
- $calendar. = "
- $currentDayRel = Str_pad ($currentDay, 2, "0", str_pad_left);
- $date = "$year-$month-$currentDayRel";
- $calendar. = "
- Counter
- $currentDay + +;
- $dayOfWeek + +;
- }
- The last row of table processing, often the last line is not all filled, need to fill the space.
- if ($dayOfWeek! = 7) {
- $remainingDays = 7-$dayOfWeek;
- $calendar. = "
- $calendar. = "
";
$monthName $year
";
";
$day | ";
";
| ";
";
$currentDay | ";
| ";
";
";
- return $calendar;
- }
- ?>
Copy CodeExample of a call to the PHP calendar code:
- PHP Calendar
- $dateComponents = getdate ();
- $month = $dateComponents [' mon '];
- $year = $dateComponents [' Year '];
- Echo Build_calendar ($month, $year, $dateArray);
- ?>
Copy Code>>> you may be interested in the article: PHP Calender Calendar code (solve 2038 problems) PHP Calendar code sharing simple and practical PHP calendar code PHP Calendar code PHP Implementation of efficient calendar code PHP calendar code Three good PHP calendar code to share a sample PHP simple calendar code PHP simple Calendar Implementation code (bindable events) |