php簡單的日歷程序代碼_第1頁
php簡單的日歷程序代碼_第2頁
php簡單的日歷程序代碼_第3頁
php簡單的日歷程序代碼_第4頁
php簡單的日歷程序代碼_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

php簡單的日歷程序代碼PHP提供了date()函數(shù),該函數(shù)提供了豐富的日期處理功能。現(xiàn)在需要獲得的數(shù)據(jù)有兩個(gè),第一個(gè)是當(dāng)月的總天數(shù);第二個(gè)是該月的第一天所在星期中的第幾天,數(shù)字表示0(表示星期天)到6(表示星期六)。通過date()函數(shù)可以很容易獲得上面的數(shù)據(jù)

代碼如下復(fù)制代碼<?php$month=$_GET['m']?$_GET['m']:date(‘n’);

$year=$_GET['y']?$_GET['y']:date(‘Y’);$start_week=date(‘w’,mktime(0,0,0,$month,1,$year));

$day_num=date(‘t’,mktime(0,0,0,$month,1,$year));

$end=false;

?>

<table>

<tr>

<td>星期日</td><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td>

</tr>

<tr>

<?php

for($i=0;$i<$start_week;$i++)

{

echo“<td></td>”;

}$j=1;while($j<=$day_num)

{

echo“<td>$j</td>”;

$week=($start_week+$j-1)%7;if($week==6){

echo“nt</tr>n”;

if($j!=$day_num)

echo“t<tr>ntt”;

else$end=true;

}

$j++;

}

while($week%7!=6)

{

echo“<td></td>”;

$week++;

}

if(!$end)

echo“n</tr>”;

?></table>高級一點(diǎn)類

代碼如下復(fù)制代碼<?php

classCalendar

{

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($optionsas$key=>$value){

if(array_key_exists($key,$vars)){

$this->$key=$value;

}

}

}

functiondisplay()

{

echo'<tableclass="calendar">';

$this->showChangeDate();

$this->showWeeks();

$this->showDays($this->year,$this->month);

echo'</table>';

}

privatefunctionshowWeeks()

{

echo'<tr>';

foreach($this->weeksas$title)

{

echo'<th>'.$title.'</th>';

}

echo'</tr>';

}

privatefunctionshowDays($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'<tdclass="today">'.$j.'</td>';

}else{

echo'<td>'.$j.'</td>';

}

if($i%7==0){

echo'</tr><tr>';

}

}

echo'</tr>';

}

privatefunctionshowChangeDate()

{

$url=basename($_SERVER['PHP_SELF']);

echo'<tr>';

echo'<td><ahref="?'.$this->preYearUrl($this->year,$this->month).'">'.'<<'.'</a></td>';

echo'<td><ahref="?'.$this->preMonthUrl($this->year,$this->month).'">'.'<'.'</a></td>';

echo'<tdn="3"><form>';

echo'<selectname="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'<selectname="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><ahref="?'.$this->nextMonthUrl($this->year,$this->month).'">'.'>'.'</a></td>';

echo'<td><ahref="?'.$this->nextYearUrl($this->year,$this->month).'">'.'>>'.'</a></td>';

echo'</tr>';

}

privatefunctionpreYearUrl($year,$month)

{

$year=($this->year<=1970)?1970:$year-1;

return'year='.$year.'&month='.$month;

}

privatefunctionnextYearUrl($year,$month)

{

$year=($year>=2038)?2038:$year+1;

return'year='.$year.'&month='.$month;

}

privatefunctionpreMonthUrl($year,$month)

{

if($month==1){

$month=12;

$year=($year<=1970)?1970:$year-1;

}else{

$month--;

}

return'year='.$year.'&month='.$month;

}

privatefunctionnextMonthUrl($year,$month)

{

if($month==12){

$month=1;

$year=($year>=2038)?2038:$year+1;

}else{

$month++;

}

return'year='.$year.'&month='.$month;

}

}調(diào)用方法

代碼如下復(fù)制代碼<?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>

<metahttp-equiv="Content-Type"content="text/html"charset="UTF-8"/>

<styletype="text/css">

table.calendar{

border:1pxsolid#050;

}

.calendarth,.calendartd{

width:30px;

text-align:center;

}

.calendarth{

background-color:#050;

color:#fff;

}

.today{

color:#fff;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論