php万年历

<script type="text/javascript" src="../../03-jQuery/js/jquery-1.10.2/jquery.js"></script>
<?php
header("Content-type:text/html;charset=utf-8");
/**
* Created by PhpStorm.
* User: lanouhn
* Date: 2017/8/24
* Time: 10:10
*/
//获取当前的年,
$year = isset($_GET['year']) && !empty($_GET['year'])?$_GET['year']:date('Y');
$month = isset($_GET['month']) && !empty($_GET['month'])?$_GET['month']:date('m');
//if (!is_numeric($year) || !is_numeric($month)){
// echo "<script>alert('请输入数字')</script>";
// $year = date('Y');
// $month = date('m');
//}
//判断下一月
if($month==12){
//12
$next_month=1;
$next_year = $year+1;
}else{
//1-11
$next_month=$month+1;
$next_year = $year;
}

//判断上一月
if($month==1){
//1
$last_month=12;
$last_year = $year-1;
}else{
//2-12
$last_month=$month-1;
$last_year = $year;
}

//获取当月一共多少天
$days = date('t');

//获取本月一号对应的是星期几 ,也就是说前面要有几个空td
$weeks = date('w',mktime(0,0,0,$month,1,$year));

//获取当前月份的第几天
$day1= date('d');

//获取上个月的总天数
//$last_month = $month - 1;
$stamp = mktime(0,0,0,$last_month,1,$year);
$last_days =date('t',$stamp);

//判断空几格
if ($weeks>0){
$l=$weeks-1;
}else{
$l=6;
}

$temp = $l;
?>

<style>
.last_month{
color: #A9A9A9;
}
td{
border: 1px solid #646464;
}
td:hover{
border: 1px solid #ffd139;
}
#btn,.btn{
border-radius: 5px;
background-color: #ffd139;
border-color: #f3e89b;
}
caption a{
text-decoration-line: none;
color: #363636;
font-weight: bold;
}
caption a:hover{
color: #6e6e6e;
}
#year,#month{
border-radius: 3px;
background-color: rgba(255, 244, 164, 0.61);
border-color: #f3e89b;
}
</style>

<!--表单-->
<form action="10-Almanac.php" id="form1">
<input type="text" name="year" placeholder="请输入年" value="<?php echo $year?>" id="year">
<input type="text" name="month" placeholder="请输入月" value="<?php echo $month?>" id="month">
<input type="button" value="提交" id="btn">
</form>

<table width="500" border="1" cellspacing="0" cellpadding="0" style="text-align: center " >
<caption style="text-align: left;"><?php echo $year; ?><?php echo $month; ?><a href="10-Almanac.php" style="float: right">刷新</a></caption>
<tr bgcolor="#bdb76b">
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<!--{里面加空格?> 表示1号对应的星期几 前面要空格-->
<?php

//上个月天数
for($j=$last_days-$l+1;$j<=$last_days;$j++){ ?>
<td class="last_month"><?php echo $j; ?></td>
<?php } ?>

<!--for 计算当前月份天数-->
<?php for($i=1;$i<=$days;$i++){

//添加背景颜色
if($i==$day1){
$color = "pink";
}else{
$color = "#999";
}?>

<!--判断换行-->
<?php $l++; ?>
<?php if($l%7==0){ ?>
<td bgcolor="<?php echo $color?>"><?php echo $i; ?></td>
</tr>
<tr>
<?php }else { ?>
<td bgcolor="<?php echo $color?>"><?php echo $i;?></td>
<?php } ?>
<?php } ?>

<!--获取下个月剩余天数-->
<?php
if($temp+$days<=35){
$total=35;
}else{
$total=42;
}
?>
<?php for($z=1;$z<=$total-$l;$z++){
?>
<?php if($l % 7 == 0){?>
<td class="last_month" ><?php echo $z;?></td>
</tr>
<tr>
<?php }else{?>
<td class="last_month"><?php echo $z;?></td>
<?php }}?>
</tr>
<!--上一页 下一页-->
<tr>
<td colspan="7">
<a href="10-Almanac.php?month=<?php echo $last_month?>&year=<?php echo $last_year?>"><button class="btn">上一月</button></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="10-Almanac.php?month=<?php echo $next_month?>&year=<?php echo $next_year?>"><button class="btn">下一月</button></a>
</td>
</tr>
</table>


<script>
$('#btn').click(function () {
$('#form1').submit();
var year = $('#year').val();
var month = $('#month').val();
year = Number(year);
month = Number(month);
if( isNaN(year) || year == 0){
alert('请输入正确年份!');
return false;
}
if( isNaN(month) || month == 0){
alert('请输入正确月份!');
return false;
}
})
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值