普通万年历效果

<?php
//1.初始化年月信息 最后返回年、月、月中天数、 月中1号是星期几
function construct(){
//初始化年月
$year = isset($_GET['year'])?$_GET['year']:date('Y');
$month = isset($_GET['month'])?$_GET['month']:date('m');
//获取对应年份中月有多少天
$day = date('t',mktime(0,0,0,$month,1,$year));
//获取对应年份中月的1号是星期几
$w = date('w',mktime(0,0,0,$month,1,$year));
return $year.','.$month.','.$day.','.$w;
//2017,09,30,5
}
//2.根据上面的函数返回信息 遍历万年历表格
function myshow(){
//需要年月以及月份中天数和月份中1号是星期几
$result = construct();
//将获取到的字符串分割成数组
list($year,$month,$day,$w) = explode(',',$result);
echo '<table border="1" align="center" width="800">';
echo '<caption>'.$year.'年'.$month.'</caption>';
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>';
$num = 1;
while($num <= $day){
echo '<tr>';
for ($i=0; $i < 7; $i++) {
if($num>$day || ($w>$i && $num ==1)){
echo '<td>&nbsp;</td>';
}else{
echo '<td>'.$num.'</td>';
$num ++;
}
}
echo '</tr>';
}
echo '<tr>';
echo '<td colspan="7" align="center">'.chageDate($year,$month).'</td>';
echo '</tr>';

}
myshow();
//
//3.显示上一年、上一月、下一年、下一月 效果
function chageDate($year,$month){
$out = '<a href="?'.preYear($year,$month).'">《上一年</a>&nbsp;&nbsp;';
$out .= '<a href="?'.preMonth($year,$month).'">《《上一月</a>&nbsp;&nbsp;';
$out .= '<a href="?'.nextMonth($year,$month).'">下一月》》</a>&nbsp;&nbsp;';
$out .= '<a href="?'.nextYear($year,$month).'">下一年》</a>';
return $out;
}

//echo chageDate();
//4.判断处理上一年函数 当前年-1 月份保持不变
function preYear($year,$month){
$year -= 1;
if($year < 1970){
$year = 1970;
}
return "year={$year}&month={$month}";
}
//
//5.判断上一月
function preMonth($year,$month){
//月份判断
if($month == 1){
$year -= 1;
//最小年的验证
if($year < 1970){
$year = 1970;
$month = 1;
}else{
$month = 12;
}
}else{
$month --;
}
return "year={$year}&month={$month}";
}
//6.判断下一月
function nextMonth($year,$month){
if($month == 12){
$year ++;
$month = 1;
}else{
//最大年时月份只能为1月
if($year == 2038){
$month =1;
}else{
$month ++;
}
}
return "year={$year}&month={$month}";
}
//7.判断下一年
function nextYear($year,$month){
$year ++;
//验证最大年
if($year >= 2038){
$year = 2038;
$month = 1;
}
return "year={$year}&month={$month}";
}

猜你喜欢

转载自www.cnblogs.com/wutianfei/p/9024248.html