Home  >  Article  >  Backend Development  >  PHP calendar code (with demonstration effect)

PHP calendar code (with demonstration effect)

WBOY
WBOYOriginal
2016-07-25 08:56:051713browse
  1. function build_calendar($month,$year,$dateArray) {
  2. // Calendar header, starting from Sunday to Saturday
  3. $daysOfWeek = array('S','M',' T','W','T','F','S');
  4. // The position of the first day of this month
  5. $firstDayOfMonth = mktime(0,0,0,$month,1,$year );
  6. // Get the number of days in this month
  7. $numberDays = date('t',$firstDayOfMonth);
  8. // Get the first day of this month
  9. $dateComponents = getdate($firstDayOfMonth);
  10. // Get the month English words for
  11. $monthName = $dateComponents['month'];
  12. $dayOfWeek = $dateComponents['wday'];
  13. // Monthly calendar header
  14. $calendar = "" ;
  15. $calendar .= "
  16. ";
  17. $calendar .= "
  18. ";
  19. // Week header
  20. foreach($daysOfWeek as $day) {
  21. $calendar .= "
  22. ";
  23. }
  24. // Start outputting the calendar
  25. // Initialize the day counter, starting from the 1st
  26. $currentDay = 1;
  27. $calendar .= "
  28. ";
  29. // Using the variable $dayOfWeek can ensure accurate output of seven days a week
  30. if ($dayOfWeek > 0) {
  31. $calendar .= "
  32. ";
  33. }
  34. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  35. while ($currentDay <= $numberDays) {
  36. // Every 7 days, add a new row after every 7 days
  37. if ($dayOfWeek == 7) {
  38. $dayOfWeek = 0;
  39. $calendar .= "
  40. ";
  41. }
  42. $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
  43. $date = "$year-$month-$currentDayRel";
  44. $calendar .= "
  45. ";
  46. // Counter
  47. $currentDay++;
  48. $dayOfWeek++;
  49. }
  50. // To process the last row of the table, it is often impossible to fill the last row completely. Fill in spaces.
  51. if ($dayOfWeek != 7) {
  52. $remainingDays = 7 - $dayOfWeek;
  53. $calendar .= "
  54. ";
  55. }
  56. $calendar .= "
  57. ";
  58. $calendar .= "
  59. $monthName $year
    $day
     
    $currentDay  
    ";
  60. return $calendar;
  61. }
  62. ?>
Copy code

php calendar code calling example :

  1. //php calendar
  2. $dateComponents = getdate();
  3. $month = $dateComponents['mon'];
  4. $year = $dateComponents['year'];
  5. echo build_calendar ($month,$year,$dateArray);
  6. ?>
Copy code

>>> Articles you may be interested in: php calender calendar code (solve 2038 problem) php calendar code sharing simple and practical php calendar code php calendar code efficient calendar code implemented in php php calendar code Three good php calendar codes Share an example of PHP simple calendar code PHP simple calendar implementation code (can bind events)



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn