PHP Calendar code (with demo effect)

Source: Internet
Author: User
  1. function Build_calendar ($month, $year, $dateArray) {
  2. Calendar header, beginning in Sunday until Saturday
  3. $daysOfWeek = Array (' s ', ' M ', ' t ', ' W ', ' t ', ' F ', ' s ');
  4. Location of the first day of the month
  5. $firstDayOfMonth = Mktime (0,0,0, $month, 1, $year);
  6. Get the number of days this month
  7. $numberDays = date (' t ', $firstDayOfMonth);
  8. Get the first day of the month
  9. $dateComponents = getdate ($firstDayOfMonth);
  10. Get the English word of the month
  11. $monthName = $dateComponents [' Month '];
  12. $dayOfWeek = $dateComponents [' wday '];
  13. Month table Header
  14. $calendar = "
  15. $calendar. = "
  16. $calendar. = "
  17. }
  18. Use variable $dayOfWeek to ensure accurate output seven days a week
  19. }
  20. }
  21. }
  22. $calendar. = "
  23. Week table Header
  24. foreach ($daysOfWeek as $day) {
  25. $calendar. = "
  26. Start Output Calendar
  27. Initialize the number of days counter, starting with number 1th
  28. $currentDay = 1;
  29. $calendar. = "
  30. if ($dayOfWeek > 0) {
  31. $calendar. = "
  32. $month = Str_pad ($month, 2, "0", str_pad_left);
  33. while ($currentDay <= $numberDays) {
  34. 7 day line, 7 days one to add a row
  35. if ($dayOfWeek = = 7) {
  36. $dayOfWeek = 0;
  37. $calendar. = "
  38. $currentDayRel = Str_pad ($currentDay, 2, "0", str_pad_left);
  39. $date = "$year-$month-$currentDayRel";
  40. $calendar. = "
  41. Counter
  42. $currentDay + +;
  43. $dayOfWeek + +;
  44. }
  45. The last row of table processing, often the last line is not all filled, need to fill the space.
  46. if ($dayOfWeek! = 7) {
  47. $remainingDays = 7-$dayOfWeek;
  48. $calendar. = "
  49. $calendar. = "
  50. "; "; "; "; "; "; "; "; "; ";
    $monthName $year
    $day
    $currentDay
    ";
  51. return $calendar;
  52. }
  53. ?>
Copy Code

Example of a call to the PHP calendar code:

    1. PHP Calendar
    2. $dateComponents = getdate ();
    3. $month = $dateComponents [' mon '];
    4. $year = $dateComponents [' Year '];
    5. Echo Build_calendar ($month, $year, $dateArray);
    6. ?>
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)

  • 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.