怎麼使用PHP實作簡單的日曆程式?這篇文章就透過一個程式碼範例來大家了解一下使用PHP實作簡單日曆程式的方法,希望對大家有幫助。
說到對日期和時間的處理,就一定要介紹一下日曆程序的編寫,大多數讀者可能都會認為日曆的作用只是為了在頁面上顯示當前的日期,其實不然,日曆在我們的開發中有著更重要的作用。例如,我們開發一個「記事本」就需要透過日曆設定日期,另外在一些系統中需要按日期去安排任務也需要用到日曆等等。
本節的範例中涉及的日期和時間函數並不是很多,都是前面介紹的,主要是透過編寫一個日曆類別來鞏固一下前面介紹的物件導向,以及時間函數應用,同時範例中涉及一些前端知識,有興趣的讀者可以閱讀本站提供的 HTML教程 和 CSS 教程。
完整的範例程式碼如下:
<?php class Calendar{ private $year, $month, $start_week, $days; /** * 构造方法,用来初始化一些日期属性 */ function __construct(){ $this->year = isset($_GET['year'])?$_GET['year']:date('Y'); $this->month = isset($_GET['month'])?$_GET['month']:date('m'); $this->start_week = date('w', mktime(0, 0, 0, $this->month, 1, $this->year)); $this->days = date('t', mktime(0, 0, 0, $this->month, 1, $this->year)); } /** * 魔术方法,用来打印整个日历 * @return string [日历的html代码] */ function __toString(){ $output = ''; $output = '<table>'; $output .= $this->changeDate(); $output .= $this->weeksList(); $output .= $this->daysList(); $output .= '</table>'; return $output; } /** * 输出周列表 * @return [string] [html 代码] */ private function weeksList($output=''){ $week = array('日','一','二','三','四','五','六'); $output .= '<tr>'; for ($i=0; $i < count($week); $i++) { $output .= '<th>'.$week[$i].'</th>'; } $output .= '</tr>'; return $output; } /** * 输出日期列表 * @return [string] */ private function daysList($output=''){ $output .= '<tr>'; for ($i=0; $i < $this->start_week; $i++) { $output .= '<td> </td>'; } for ($j=1; $j <= $this->days; $j++) { $i++; if($j == date('d') && $this->year == date('Y') && $this->month == date('m')){ $output .= '<td>'.$j.'</td>'; }else{ $output .= '<td>'.$j.'</td>'; } if($i%7 == 0) $output .= '</tr><tr>'; } while($i%7 !== 0){ $output .= '<td> </td>'; $i++; } $output .= '</tr>'; return $output; } /** * 处理上一年的数据 * @param [type] $year [年份] * @param [type] $month [月份] * @return [type] [description] */ private function prevYear($year, $month){ $year -= 1; if($year < 1970) $year = 1970; return "year=$year&month=$month"; } /** * 处理上一月的数据 * @param [type] $year [年份] * @param [type] $month [月份] * @return [type] [description] */ private function prevMonth($year, $month){ if($month == 1){ $year -= 1; if($year < 1970) $year = 1970; $month = 12; }else{ $month--; } return "year=$year&month=$month"; } /** * 处理下一年的数据 * @param [type] $year [年份] * @param [type] $month [月份] * @return [type] [description] */ private function nextYear($year, $month){ $year += 1; if($year > 2038) $year = 2038; return "year=$year&month=$month"; } /** * 处理下一月的数据 * @param [type] $year [年份] * @param [type] $month [月份] * @return [type] [description] */ private function nextMonth($year, $month){ if($month == 12){ $year --; if($year > 2038) $year = 2038; $month = 1; }else{ $month++; } return "year=$year&month=$month"; } /** * 调整年份和月份 * @param string $output [html代码] * @param string $url * @return [type] */ private function changeDate($output='', $url='index.php'){ $output .= '<tr>'; $output .= '<td><a href="'.$url.'?'.$this->prevYear($this->year, $this->month).'">'.'<<'.'</a></td>'; $output .= '<td><a href="'.$url.'?'.$this->prevMonth($this->year, $this->month).'">'.'<'.'</a></td>'; $output .= '<td colspan="3">'; $output .= '<form>'; $output .= '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">'; for ($i=1970; $i <=2038; $i++) { $selected = ($i == $this->year)?'selected="selected"':''; $output .= '<option value="'.$i.'" '.$selected.'>'.$i.'</option>'; } $output .= '</select>'; $output .= '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">'; for ($j=1; $j <=12; $j++) { $selected = ($j == $this->month)?'selected="selected"':''; $output .= '<option value="'.$j.'" '.$selected.'>'.$j.'</option>'; } $output .= '</select>'; $output .= '</form>'; $output .= '</td>'; $output .= '<td><a href="'.$url.'?'.$this->nextMonth($this->year, $this->month).'">'.'>'.'</a></td>'; $output .= '<td><a href="'.$url.'?'.$this->nextYear($this->year, $this->month).'">'.'>>'.'</a></td>'; $output .= '</tr>'; return $output; } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>PHP实现简单的日历程序</title> <style> table{ border: 1px solid #ccc; } .fontb{ color: white; background: blue; } th{ width: 30px; } td,th{ height:30px; text-align: center; } form{ margin: 0px; padding: 0px; } </style> </head> <body> <?php $calendar = new Calendar; echo $calendar; ?> </body> </html>
運作結果如下圖:
推薦學習:《PHP影片教學》

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。