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

A case of implementing a simple calendar in PHP

Jun 01, 2018 pm 05:22 PM
php calendar Case

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

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles