您的位置:首页 > 编程语言 > PHP开发

修改后的php万年历

2017-08-26 14:27 232 查看
<?php

header("content-type:text/html;charset=utf-8");

/**

* Created by PhpStorm.

* User: zk

* Date: 2017/8/25

* Time: 20:34

*/

?>

<?php

//------获取form表单里的年月,并判断是否为空,为空输出当前,不为空表单输出--------

$year = isset($_GET["year"]) && !empty($_GET["year"])?$_GET["year"]:date("Y");

$month = isset($_GET["month"]) && !empty($_GET["month"])?$_GET["month"]:date("m");

//判断form表单的年月,是否为输入正确

if(!is_numeric($year) || !is_numeric($month)){

$year = date("Y");

$month = date("m");

}

$next_month = $month +1;

if($next_month==13){

$next_month = 1;

$next_year = $year + 1;

}else{

$next_month = $next_month;

$next_year = $year;

}

//$last_year = $year - 1;

$last_month = $month -1;

if($last_month== 0){

$last_month=12;

$last_year = $year -1;

}else{

$last_month = $month - 1;

$last_year = $year;

}

//获取当前的年份月份天数

//$year = date("Y");

//$month = date("m");

$days = date("t",mktime(0,0,0,$month,1,$year));//给定月份应有的天数

//获取当前的具体哪一天

$day = date("d");

//获取上个月,和上个月的天数

//$last_month = $month -1;

//获取上个月的时间戳

$temp = mktime(0,0,0,$month-1,1,$year);

$last_days = date("t",$temp);

//获取每月1号对应的星期几

$week = date("N",mktime(0,0,0,$month,1,$year));

//前边的空格数,和前边空格子填得第一个数

$empty_next = $week - 1;

$num_next = $last_days-$empty_next;

//后边的空格数

$num_last = 42 - $days - $empty_next;

?>

<!--css样式添加-->

<style>

tr>th {

background-color: pink;

}

.days {

text-align: center;

background-color: dimgrey;

}

.day_now {

background-color: hotpink;

border:1px solid aquamarine;

color:white;

}

.num_n_l {

text-align: center;

background-color: lightgrey;

}

.a_next {

float: left;

}

.a_last {

float: right;

}

a {

text-decoration: none;

}

#td_form {

width: 500px;

height: 20px;

padding-top: 10px;

background-color: silver;

}

#year,#month {

width: 200px;

height: 30px;

}

</style>

<script src="js/jquery.min.js"></script>

<!--table编写-->

<table border="1" width="500" cellpadding="0" cellspacing="0" align="center">

<caption>万年历<?php echo $year."年".$month."月"?></caption>

<tr>

<td colspan="7" id="td_form">

<!-------------------------form表单-------------------------------->

<form action="" method="get" id="form">

<input type="text" name="year" id="year" placeholder="请输入年份" value="<?php echo $year;?>">

<input type="text" name="month" id="month" placeholder="请输入月份" value="<?php echo $month;?>">

<input type="submit" value="提交">

<a href="wannianli.php">刷新</a>

</form>

</td>

</tr>

<tr>

<th>一</th>

<th>二</th>

<th>三</th>

<th>四</th>

<th>五</th>

<th>六</th>

<th>日</th>

</tr>

<tr>

<?php

//前边的空格子

for($j=0;$j<$empty_next;$j++){

$num_next++;?>

<td class="num_n_l"><?php echo $num_next;?></td>

<?php }

?>

<?php

for($i=1;$i<=$days;$i++){?>

<?php

//判断当前日期

if($i==$day){

$class = "day_now";

}else{

$class = "";

}

?>

<?php if(($i+$empty_next)%7==0){?>

<td class="days <?php echo $class;?>"><?php echo $i;?></td>

</tr>

<tr>

<?php }else{?>

<td class="days <?php echo $class;?>"><?php echo $i;?></td>

<?php }

}

?>

<?php

//后边的空格子

for($k=1;$k<=$num_last;$k++){?>

<?php

if((14-$num_last+$k)%7==0){?>

<td class="num_n_l"><?php echo $k;?></td>

</tr><tr>

<?php

}else{

?>

<td class="num_n_l"><?php echo $k;?></td>

<?php }

}

?>

</tr>

<tr>

<th colspan="7">

<div class="a_next"><a href="wannianli.php?month=<?php echo $last_month;?>&year=<?php echo $last_year;?>">上一月</a></div>

<div class="a_last"><a href="wannianli.php?month=<?php echo $next_month;?>&year=<?php echo $next_year;?>">下一月</a></div>

</th>

</tr>

</table>

<!----jquery代码--需写在html代码之后,前边代码加载完才能执行------>

<script>

$("td").mouseover(function () {

$("td").css("border","1px solid");

$(this).css("border","1px solid yellow");

})

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 万年历 附图