PHP程式輸出日曆

2022-04-27 12:34:11 字數 3900 閱讀 1036

以下**只是簡單實現日曆的效果和邏輯思路,沒有使用類封裝,權當拋磚引玉,有興趣的朋友可以封裝起來,方便呼叫。

<?php /**

* php利用時間函式輸出日曆

* rain.zen $ [email protected] */

/*** 獲取關於日曆的格式對應的天數

* @param $format 獲取的格式[t:指定月份的天數; w:數字型的星期數(0-星期日,6-星期六)]

* @param $year 年份,例:2009

* @param $month 月份,例:03

* @param $day 日期,例:08

* @return bool|string 返回值是根據格式$format的選擇決定 */

function calendernum($format, $year, $month, $day)//

月份陣列

$arymonth = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二');

//當前年月日

$thentime = date('y-m-d');

list($thenyear, $thenmonth, $thenday) = explode('-', $thentime

);//

是否是星期日作為開頭[1:是; 0:否]

$issunday = 0;

//日期:年份下拉控制項

$year = isset($_post['year']) ? $_post['year'] : $thenyear

;$yearlast = 8; //

年份的前後持續週期

$selectyear = '';

for($i = ($year + $yearlast), $n = ($year - $yearlast); $i >= $n; $i--)

$selectyear.= '';

//日期:月份下拉控制項

$month = isset($_post['month']) ? $_post['month'] : $thenmonth

;$selectmonth = '';

foreach($arymonth

as$k => $v

)$selectmonth.= '';

unset($k, $v

);//

日期:星期html

$aryweek = array('一', '二', '三', '四', '五', '六');

$issunday and array_unshift($aryweek, '日');

$issunday or array_push($aryweek, '日');

$weektrhtml = '';

foreach($aryweek

as$v

)$weektrhtml.= '';

unset($v

);//

以下為計算當月的天數以及前後月份作為空位補齊日曆**

$allday = calendernum('t', $year, $month, $thenday); //

指定月份的天數

$monthfirstday2weekval = calendernum('w', $year, $month, 1); //

指定日期的星期數

$aryday = range(1, $allday

);//

計算第一排**空位數以及是否需要補齊

$each = $issunday ? 7 : 6;

$firstnum = $issunday ? $monthfirstday2weekval : ($monthfirstday2weekval-1);

$isaddday = $firstnum % $each;//

用上月的最後幾天補齊空位數

if($isaddday != 0)

//計算最後一排**空位數以及是否需要補齊

$lastnum = count($aryday) % 7;

$lastnum == 0 or $arylast = range(1, (7 - $lastnum

));isset($arylast) and $aryday = array_merge($aryday, $arylast

);//

迴圈輸出日期的**

$x = count($aryday) / 7;

$y = 1;

$daytrhtml = '';

foreach($aryday

as$i => $rsday)}

$daytrhtml.= '';

?>

模板html**

doctype html

>

<

html

>

<

head

>

<

meta

charset

="utf-8"

/>

<

title

>php程式輸出日曆

title

>

<

style

type

="text/css"

>

/* global css document => author:[email protected] */

htmlbody

body,h1,h2,h3,h4,h5,h6,p,div,dl,dt,dd,ul,ol,li,form,button,input,textarea,th,td

h1,h2,h3,h4,h5,h6,select,input,textarea,button,table

ul,ol,dlaui,em

a:hovera imga,select,input,textarea.fr

tableth.fl

.cf:before,.cf:after.cf:after.cf

body

#u-calender

#u-calender #u-slt

#u-slt select

#u-calender .z-on

#u-calender .z-pass

#u-calender table

#u-calender table tr td

style

>

head

>

<

body

>

<

div

id="u-calender"

>

<

table

><

tr><

td colspan

="7"

id="u-slt"

><

form

action

=""method

="post"

>月份:

php echo $selectmonth;

?>

年份:php echo $selectyear;

?>

form

>

td>

tr>

php echo $weektrhtml.$daytrhtml;

?>

table

>

div>

body

>

html

>

**完成後實現的效果截圖:

PHP輸出日曆 PHP實現日曆效果

mdays date t 當月總天數 datenow date j 當日日期 monthnow date n 當月月份 yearnow date y 當年年份 計算當月第一天是星期幾 wk1st date w mktime 0,0,0,monthnow,1,yearnow trnum ceil md...

基於PHP的日曆程式

php日曆 函式獲取當前的年月 year get y get y date y mon get m get m date m 函式的使用,獲取當前月的天數及當月1號的星期 day date t mktime 0,0,0,mon,1,year w date w mktime 0,0,0,mon,1,y...

c語言輸出日曆

include void main else sum sum 365 按照capucivar的思路,接下來要計算從該年1月到使用者輸入月份的上乙個月的天數 for int j 1 j int a sum 7 計算出總天數跟7求餘,得到乙個餘數 int monthday z是使用者輸入月份的天數 sw...