Home >Backend Development >PHP Tutorial >A case of implementing a simple calendar in PHP

A case of implementing a simple calendar in PHP

墨辰丷
墨辰丷Original
2018-06-01 17:22:122080browse

This article mainly introduces the simple calendar implementation method in PHP, involving the related calculation operations of PHP date and time. It is very simple and practical. Friends in need can refer to it

The screenshot of the running effect is as follows:

The specific code is as follows:

<?php
/*
 * Created on 2016-7-20
 */
SimCalendar(&#39;2016-08&#39;);//显示8月份日历
function SimCalendar($date)
{
  /**
   * 简单日历输出,本函数需要cal_days_in_month的支持
   * @param $date Y-m 要输出的日期
   */
  echo &#39;<table border="1">
  <thead>
    <tr>
      <th>日</th>
      <th>一</th>
      <th>二</th>
      <th>三</th>
      <th>四</th>
      <th>五</th>
      <th>六</th>
    </tr>
  </thead>
  <tbody>&#39;;
    $date_array = explode(&#39;-&#39;, $date);
    $start_week = 0;//从星期天开始为0
    $month = cal_days_in_month(CAL_GREGORIAN, $date_array[1], $date_array[0]);//当月的天数
    $wstar = date(&#39;w&#39;, strtotime($date . &#39;-01&#39;));//当月从星期几天始
    $rows = ceil(($wstar + $month) / 7);//总行数
    $mday = 1;//第几天
    for ($i = 0; $i < $rows; $i++) {
      echo &#39;<tr>&#39;;
      for ($d = 0; $d < 7; $d++) {
        $nowday = 7 * $i + $d + $start_week;
        if ($nowday >= $wstar && $mday <= $month) {
          $temp = date(&#39;d&#39;, strtotime($date . &#39;-&#39; . $mday));
          echo &#39;<td>&#39;.$temp . &#39;</td>&#39;;
          $mday++;
        } else {
          echo &#39;<td> </td>&#39;;
        }
      }
      echo &#39;</tr>&#39;;
    }
  echo &#39;</tbody>
</table>&#39;;
}
?>

Summary: The above is this article The entire content, I hope it will be helpful to everyone's study.

Related recommendations:

php7’s new feature usage detailed explanation

Detailed explanation of PHP post-static Binding analysis and application

Comparison of JavaScript and PHP dynamically adding methods to classes

##

The above is the detailed content of A case of implementing a simple calendar in PHP. For more information, please follow other related articles on the PHP Chinese website!

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