สวัสดีครับวันนี้เรามาทำปฏิทินต่อจากบทความที่ผ่านมา ซึ่งบทความตอนที่แล้วนั้นเราได้ใส่สีสันให้กับปฏิทินเราด้วยเทคนิค CSS จนมีหน้าตาสวยงามไปแล้วนะครับ ดูได้จาบทความนี้นะครับ
สำหรับวันนี้เราก็จะมาทำการเพิ่มเติมรูปแบบปฏิทินของเราให้มีฟังก์ชั่นที่น่าใช้งานมากยิ่งขึ้นอีก นั่นคือการเพิ่มการเลื่อนแสดงปฏิทินไปเดือนที่ผ่านมา / เดือนก่อนหน้านะครับ
หลักการทำงานของเราก็ง่ายๆเพียงเราเพิ่มลิงค์ที่ใช้สำหรับการเลื่อนดูปฏิทินที่ส่วนหัวของตาราง แล้วก็เก็บค่า Timestamp ของเดือนนั้นๆไว้มาทำงานต่อในส่วนของการสร้างปฏิทินนะครับ เรามาลุยกันเลย
เริ่มต้นก็ขอให้คัดลอกโค้ดนี้ไปก่อนนะครับ แล้วเรามาอธิบายโค้ดคำสั่งต่างๆกัน
ไฟล์ calendar_3.php
<html>
<head>
<title>ปฏิทินเบื้องต้น</title>
<style>
.sunday{color: white;background-color:red;}
.saturday{color: white;background-color:#9900CC;}
.default{color: black;background-color:white;}
.today{color: blue;background-color:yellow;}
</style>
</head>
<body>
<?
$now = strtotime("now");
if(isset($_GET['now']) && !empty($_GET['now'])){
$now = $_GET['now'];
}
$month = date('n',$now);
$year = date('Y',$now);
$first = strtotime("$year-$month-1");
$first_day = date('w',$first);
$num_day = date('t',$now);
$today = date("Y-n-j");
$TH_Day = array("อา.","จ.","อ.","พ.","พฤ.","ศ.","ส.");
$TH_Month = array(1 => "มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน",
"กรกฏาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
$TH_Year = $year+543;
$class = "default";
$url_togo = $_SERVER['PHP_SELF'] . "?now=";
$last_month = strtotime("-1 month $year-$month");
$next_month = strtotime("+1 month $year-$month");
$url_lastMonth = "<a href=\"" . $url_togo . $last_month . "\">«</a>";
$url_nextMonth = "<a href=\"" . $url_togo . $next_month . "\">»</a>";
echo("<table border=0 cellspacing=1 cellpadding=7 bgcolor=#FFCC99>
<tr>
<th>$url_lastMonth</th>
<th colspan=5>พุทธศักราช $TH_Year<p>$TH_Month[$month]</th>
<th>$url_nextMonth</th>
</tr>
<tr bgcolor=#FFFACD><th>" . implode("</th><th>",$TH_Day) . "</th></tr>");
$start = 1 - $first_day;
$w = 1;
for($d = $start; ;$d++){
$class = "default";
$date = "$year-$month-$d";
if($w == 1){
$class = "sunday";
echo("<tr align=center class=$class>");
}else if($w == 7){
$class = "saturday";
}else{
$class = "default";
}
if($d < 1){
echo("<td class=$class> </td>");
}else if($d >= $num_day){
if($d == $num_day){
if($today == $date){
$class = "today";
}
echo("<td align=center class=$class>$d</td>");
}else{
if($w == 7){
$class = "saturday";
}else{
$class = "default";
}
echo("<td class=$class> </td>");
}
if($w == 7){
echo("</tr>");
break;
}
}else{
if($today == $date){
$class = "today";
}
echo("<td align=center class=$class>$d</td>");
}
if($w == 7){
echo("</tr>");
$w = 1;
}else{
$w++;
}
}
?>
</table>
</body>
</html>
อธิบายการทำงาน
ผมได้นำไฟล์เดิมจาก calendar_2.php ในบทความตอนที่แล้วมาทำการแก้ไขเพิ่มเติมนะครับ โดยผมขออธิบายในส่วนที่เพิ่มเติมเข้ามานะครับ (ผมปรับปรุงการแสดงผลนิดหน่อย หาดูเองนะไม่ยาก อิอิ)
ขั้นแรกเวลาเราจะสร้างปฏิทินโดยสามารถเลื่อนไปเดือนก่อนหน้า และเดือนถัดไปได้นั้น เราจะต้องมีตัวแปรตัวหนึ่งมาเก็บค่า Timestamp ของเดือนนั้นๆได้
$now = strtotime("now");
if(isset($_GET['now']) && !empty($_GET['now'])){
$now = $_GET['now'];
}