PHP執行個體————萬年曆

來源:互聯網
上載者:User

標籤:php   css   萬年曆   

和大家分享一個簡易的萬年曆製作過程。

基本要求:

1.擷取日期

2.擷取給定日期是幾號

3.擷取給定日期是周幾

4.擷取月份天數

5.擷取上一月和下一月

先貼一張,樣式做的比較醜,不喜勿噴。


php代碼:

<?php//修改字元編碼header("content-type:text/html;charset=utf-8");//外部樣式連結echo "<link rel='stylesheet' type='text/css' href='calendar.css'/>";//擷取當前年$year=$_GET['Y']?$_GET['Y']:date('Y');//擷取當前月$month=$_GET['m']?$_GET['m']:date('m');//擷取當月有多少天$days=date('t',strtotime("{$year}-{$month}-1"));//當前是周幾$week=date('w',strtotime("{$year}-{$month}-1"));//內容置中顯示echo "<center>";//列印表頭echo "<h1>{$year}年{$month}月</h1>";//列印日期表格echo "<table>";//列印星期echo "<tr>";echo "<th>周日</th>";echo "<th>周一</th>";echo "<th>周二</th>";echo "<th>周三</th>";echo "<th>周四</th>";echo "<th>周五</th>";echo "<th>周六</th>";echo "</tr>";//列印幾號for($i=1-$week;$i<=$days;){echo "<tr>";for($j=0;$j<7;$j++){if($i>$days||$i<1){echo "<td> </td>";}else{echo "<td>$i</td>";}$i++;}echo "</tr>";}echo "</table>";//上一月和下一月演算法if($month==1){$prevyear=$year-1;$prevmonth=12;}else{$prevyear=$year;$prevmonth=$month-1;}if($month==12){$nextyear=$year+1;$nextmonth=1;}else{$nextyear=$year;$nextmonth=$month+1;}//上一月和下一月的超連結echo "<h2><a href='indextest.php?Y={$prevyear}&m={$prevmonth}'>上一月</a>&<a href='indextest.php?Y={$nextyear}&m={$nextmonth}'>下一月</a></h2>";echo "</center>";?>

CSS代碼:

table{width:500px;height:300px;border:red dashed 1px;background:#ff00ff;}tr{text-align:center;}td{border:gray dotted 1px;}h1{font-style:italic;font-size:50px;font-family:'宋體';}h2 a{font-style:normal;font-size:40px;font-family:'黑體';color:purple;}/*組合選取器*/tr,td,th{font-size:20px;background:gray;}

說幾點比較容易出錯和用法巧妙的地方:

1.strtotime()中所計算的時間戳記應該是完整的一個格式,把一個單獨的年或月放進去是沒有用的。

2.if語句中的條件裡面,不是賦值,是等於!!!,要寫 兩個==。這地方太容易給忽略了。

3.原本列印的日期一直都是周日和一號對應。但是月份不同,這個日期和星期的對應關係也會有所改變,所以,在for迴圈中將$i-$week.就可以將當月的所有日期後退一定時間,達到日期與星期的完美對應。

4.在實現上一月和下一月的功能時,將幾個變數作為參數帶入,再配合一定的演算法就能搞定了。具體的就看代碼吧,文字也得再好也不如看兩行代碼效果好。


PHP執行個體————萬年曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.