PHP simple way to create a calendar _php tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.