PHP 自製日曆

2021-09-06 18:22:45 字數 3783 閱讀 7088

最近的乙個專案中,需要將資料用日曆方式顯示,網上有很多的js外掛程式,後面為了自己能有更大的控制權,決定自己製作乙個日曆顯示。如下圖所示:

1、new乙個calendar類

2、初始化兩個下拉框中的資料,年份與月份

3、初始化要搜尋的年份和月份

4、計算得出日曆中每一天的資料資訊,包括css、天數

<?php

require_once 'calendar.php';

$util = new

calendar();

$years = array(2012, 2013, 2014, 2015, 2016);//

年份選擇自定義

$months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);//

月份陣列

//獲取post的年份資料

if(empty($_post['ddlyear']))

else

//獲取post的月份資料

if(empty($_post['ddlmonth']))

else

$calendar = $util->threshold($year, $month);//

獲取各個邊界值

$caculate = $util->caculate($calendar);//

計算日曆的天數與樣式

$draws = $util->draw($caculate);//

畫**,設定table中的tr與td

?>

1、休息天的背景色是不同的,不是當前搜尋年月的天數字型顏色也是不同的

2、div中做初始化年份與月份的下拉框的操作,並選中當前要搜尋的年月

3、資料已計算好,哪個td屬於哪個tr也已做好,直接將table列印出來即可

<

div

style

="padding:20px"

>

<

select

name

="ddlyear"

>

<?

php foreach($years as $data)

?>

select

>

<

select

name

="ddlmonth"

>

<?

php foreach($months as $data)

?>

select

>

<

input

type

="submit"

value

="修改"

/>

div>

<

table

width

="100%"

cellspacing

="0"

class

="table_calendar"

>

<

thead

class

="f14"

>

<

tr>

<

td width

="16%"

>日

td>

<

td width

="14%"

>一

td>

<

td width

="14%"

>二

td>

<

td width

="14%"

>三

td>

<

td width

="14%"

>四

td>

<

td width

="14%"

>五

td>

<

td width

="14%"

>六

td>

tr>

thead

>

<

tbody

class

="f14"

>

<?

php foreach($draws as $draw)

?>

tr>

<?

php }

?>

tbody

>

table

>

1、threshold方法,生成日曆的各個邊界值

1)計算這個月總天數

2)計算這個月第一天與最後一天,各是星期幾

3)計算日曆中的第乙個日期與最後乙個日期

/*

* * @deprecated 生成日曆的各個邊界值

* @param string $year

* @param string $month

* @return array

*/function threshold($year, $month

)

2、caculate方法,計算日曆的天數與樣式

1)將上個月的天數計算出來,本月第一天的星期不是星期天的話,就需要根據上個月的最後一天計算

2)將本月的天數遍歷出來,如果是休息天就加上特殊的css樣式

3)將下個月的天數計算出來,分三種情況,星期日、星期六和工作日

/*

* * @author pwstrick

* @param array $calendar 通過threshold方法計算後的資料

* @deprecated 計算日曆的天數與樣式

*/function caculate($calendar

)

$lastdays = array_reverse($lastdays);//

反序foreach ($lastdays

as$index => $day

) }

//本月日曆資訊

for ($i = 1; $i

<= $days; $i++)

//下月日曆資訊

if($lastdayofweek == 7)

elseif($lastdayofweek == 6)

else

for ($i = 1; $i

<= $length; $i++)

return

$dates

; }

3、draw方法,畫**,設定table中的tr與td

1)資料將要用table標籤來顯示,所以這裡要將各個tr下面的td排列好

2)$index % 7 == 0 計算**每行的第一列

3)$index % 7 == 6 ||

$index == ($length-1) 計算每行的最後一列,或$caculate的最後乙個資料

4)將中間行新增到$tr中,就是每一行的array

/*

* * @author pwstrick

* @param array $caculate 通過caculate方法計算後的資料

* @deprecated 畫**,設定table中的tr與td

*/function draw($caculate

) elseif($index % 7 == 6 || $index == ($length-1)) else

}return

$result

; }

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日曆

在我目前正在進行的專案中,我們遇到了類似的挑戰.我們還希望顯示不是持久日曆專案的事件,而是來自行動計畫的專案.我們使用了full calander,結合了提供json feed的php程式設計.完整日曆還提供了許多事件掛鉤,在我們的例子中觸發了程式設計資料庫的php程式設計的ajax發布.更詳細一點...

PHP 如何自製模板

header this is header footer this is footer list array 0 array name vboy pass 123456 1 array name json pass 111111 ob start 開啟輸出緩衝區 turn on output buf...