/**
* 1.获取日期年和月,默认当前年份和月份
* 2.计算当前月份有多少天和本月1号是星期几
* 3.输出日期的头部信息
* 4.循环遍历输出输出天数
* 5.完成上一月和下一月链接
* *
*/
//1.
$year = empty($_REQUEST['y']) ? date("Y") : intval($_REQUEST['y']);
$month = empty($_REQUEST['m']) ? date("m") : intval($_REQUEST['m']);
//2.
$day = date("t",mktime(0,0,0,$month,'1',$year));//获取对应该月的天数
$moneday = date("w",mktime(0,0,0,$month,'1',$year)); //获取该月的星期几
//3.
$weekarr = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
echo "
echo "
{$year}年{$month}月
";echo "
echo "
";for($i=0;$i<=6;$i++){
echo "
{$weekarr[$i]}";}
echo "
";echo "
";//4.
$ed = 1;
while($ed <= $day){
//每七天换行
echo "
";for($i=0;$i<=6;$i++){
//不能超过当月的天数且还没有该输出日期的时候
if($ed <= $day && ($moneday <= $i || $ed != 1) ){
echo "
{$ed}";$ed++;
}else{
echo "
";}
}
}
echo "
";echo "
";//5
$prey = $nexty = $year;//年
$prem = $nextm = $month;//月
if($prem <= 1){
$prem = 12;
$prey--;
}else{
$prem--;//上一月
}
if($nextm >= 12){
$nextm = 1;
$nexty++;
}else{
$nextm++;//下一月
}
echo "
上一月 ";
echo "下一月";
echo "
来源:https://www.cnblogs.com/lzy007/p/6218652.html