用PHP生成日历
1. [文件] calendar.class.php
<?php class Calendar { private $year; private $month; private $weeks = array('日','一','二','三','四','五','六'); function __construct($options = array()) { $this->year = date('Y'); $this->month = date('m'); $vars = get_class_vars(get_class($this)); foreach ($options as $key=>$value) { if (array_key_exists($key, $vars)) { $this->$key = $value; } } } function display() { echo '<table class="calendar">'; $this->showChangeDate(); $this->showWeeks(); $this->showDays($this->year,$this->month); echo '</table>'; } private function showWeeks() { echo '<tr>'; foreach($this->weeks as $title) { echo '<th>'.$title.'</th>'; } echo '</tr>'; } private function showDays($year, $month) { $firstDay = mktime(0, 0, 0, $month, 1, $year); $starDay = date('w', $firstDay); $days = date('t', $firstDay); echo '<tr>'; for ($i=0; $i<$starDay; $i++) { echo '<td> </td>'; } for ($j=1; $j<=$days; $j++) { $i++; if ($j == date('d')) { echo '<td class="today">'.$j.'</td>'; } else { echo '<td>'.$j.'</td>'; } if ($i % 7 == 0) { echo '</tr><tr>'; } } echo '</tr>'; } private function showChangeDate() { $url = basename($_SERVER['PHP_SELF']); echo '<tr>'; echo '<td><a href="?'.$this->preYearUrl($this->year,$this->month).'">'.'<<'.'</a></td>'; echo '<td><a href="?'.$this->preMonthUrl($this->year,$this->month).'">'.'<'.'</a></td>'; echo '<td colspan="3"><form>'; echo '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">'; for($ye=1970; $ye<=2038; $ye++) { $selected = ($ye == $this->year) ? 'selected' : ''; echo '<option '.$selected.' value="'.$ye.'">'.$ye.'</option>'; } echo '</select>'; echo '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value+\'\'">'; for($mo=1; $mo<=12; $mo++) { $selected = ($mo == $this->month) ? 'selected' : ''; echo '<option '.$selected.' value="'.$mo.'">'.$mo.'</option>'; } echo '</select>'; echo '</form></td>'; echo '<td><a href="?'.$this->nextMonthUrl($this->year,$this->month).'">'.'>'.'</a></td>'; echo '<td><a href="?'.$this->nextYearUrl($this->year,$this->month).'">'.'>>'.'</a></td>'; echo '</tr>'; } private function preYearUrl($year,$month) { $year = ($this->year <= 1970) ? 1970 : $year - 1 ; return 'year='.$year.'&month='.$month; } private function nextYearUrl($year,$month) { $year = ($year >= 2038)? 2038 : $year + 1; return 'year='.$year.'&month='.$month; } private function preMonthUrl($year,$month) { if ($month == 1) { $month = 12; $year = ($year <= 1970) ? 1970 : $year - 1 ; } else { $month--; } return 'year='.$year.'&month='.$month; } private function nextMonthUrl($year,$month) { if ($month == 12) { $month = 1; $year = ($year >= 2038) ? 2038 : $year + 1; }else{ $month++; } return 'year='.$year.'&month='.$month; } }
2. [文件] demo.php
<?php $params = array(); if (isset($_GET['year']) && isset($_GET['month'])) { $params = array( 'year' => $_GET['year'], 'month' => $_GET['month'], ); } $params['url'] = 'demo.php'; require_once 'calendar.class.php'; ?> <html> <head> <title>日历demo</title> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> <style type="text/css"> table.calendar { border: 1px solid #050; } .calendar th, .calendar td { width:30px; text-align:center; } .calendar th { background-color:#050; color:#fff; } .today{ color:#fff; background-color:#050; } </style> </head> <body> <p style="align:center"> <?php $cal = new Calendar($params); $cal->display(); ?> </p> </body> </html>
3. [图片] sample.jpg
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
2 몇 주 전ByDDD
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.
