PHP菜鸟的一个小问题
这里有一段程序是我自己写的,当然获取日期和更改每月的星期还没有实现,但是为什么如果是3月,点了上一月会回到9月?10月点了下一月也一样,望高手指点!地址:http://h...
这里有一段程序是我自己写的,当然获取日期和更改每月的星期还没有实现,但是为什么如果是3月,点了上一月会回到9月?10月点了下一月也一样,望高手指点!
地址:
http://hi.baidu.com/%D0%C7%C7%E7_0907/blog/item/1e61fc3d42994cf23d6d97a2.html 展开
地址:
http://hi.baidu.com/%D0%C7%C7%E7_0907/blog/item/1e61fc3d42994cf23d6d97a2.html 展开
4个回答
展开全部
echo date("Ymd",strtotime("now")), "\n"; echo date("Ymd",strtotime("-1 week Monday")), "\n"; echo date("Ymd",strtotime("-1 week Sunday")), "\n"; echo date("Ymd",strtotime("+0 week Monday")), "\n"; echo date("Ymd",strtotime("+0 week Sunday")), "\n"; //date('n') 第几个月 //date("w") 本周周几 //date("t") 本月天数 echo '
上周:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n"; echo '
本周:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n"; echo '
上月:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n"; echo '
本月:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n"; $getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));//本季度未最后一月天数 echo '
本季度:
'; echo date('Y-m-d H:i:s', mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y'))),"\n"; echo date('Y-m-d H:i:s', mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y'))),"\n";
上周:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n"; echo '
本周:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n"; echo '
上月:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n"; echo '
本月:
'; echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n"; echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n"; $getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));//本季度未最后一月天数 echo '
本季度:
'; echo date('Y-m-d H:i:s', mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y'))),"\n"; echo date('Y-m-d H:i:s', mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y'))),"\n";
参考资料: http://hi.baidu.com/happyxlq/blog/item/74cbaed9c41ce72111df9b9c.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我帮你改了一个,完全符合你的要求:
<html>
<head>
<title>日历程序</title>
</head>
<body>
<?php
/*
//* 根据时间计算每月有多少天
*/
function getDays($month, $year)
{
switch($month)
{
case 4:
case 6:
case 9:
case 11:
$days = 30;
break;
case 2:
if ($year%4==0)
{
if ($year%100==0)
{
$days = $year%400==0 ? 29 : 28;
}
else
{
$days =29;
}
}
else
{
$days = 28;
}
break;
default:
$days = 31;
break;
}
return $days;
}
/*
//* 根据时间戳取得对应的星期几
*/
function getWeek($time_stamp)
{
$week_num = date("w",$time_stamp);
switch($week_num)
{
case "0":
$week = "日";
break;
case '1':
$week = "一";
break;
case '2':
$week = "二";
break;
case '3':
$week = "三";
break;
case '4':
$week = "四";
break;
case '5':
$week = "五";
break;
case '6':
$week = "六";
break;
}
return $week;
}
/*
//* 根据日期相加减
*/
function DateAdd($part, $number, $date)
{
$date_array = getdate(strtotime($date));
$hor = $date_array["hours"];
$min = $date_array["minutes"];
$sec = $date_array["seconds"];
$mon = $date_array["mon"];
$day = $date_array["mday"];
$yar = $date_array["year"];
switch($part)
{
case "y": $yar += $number; break;
case "q": $mon += ($number * 3); break;
case "m": $mon += $number; break;
case "w": $day += ($number * 7); break;
case "d": $day += $number; break;
case "h": $hor += $number; break;
case "n": $min += $number; break;
case "s": $sec += $number; break;
}
return date("Y-m-d H:i:s", mktime($hor, $min, $sec, $mon, $day, $yar));
}
$M = $_GET['m'];
$abs_m = abs($M);
if (is_int($abs_m) and $M <> "" )
{
$date = DateAdd('m', $M, date('Y-m-d H:i:s',strtotime(date('Y').'-'.date('m').'-15')));
$ds = explode('-',$date);
$now_M = $ds[1];
$now_Y = $ds[0];
$days = getDays((int)$now_M, (int)$now_Y);
}
else //默认当前月
{
$now_M = date('m');
$now_Y = date('Y');
$days = getDays((int)$now_M, (int)$now_Y);
}
$index = 0;
switch(getWeek(strtotime($now_Y.'-'.$now_M.'-1 00:00:00')))
{
case '日' : $index = 0;break;
case '一' : $index = 1;break;
case '二' : $index = 2;break;
case '三' : $index = 3;break;
case '四' : $index = 4;break;
case '五' : $index = 5;break;
case '六' : $index = 6;break;
}
echo $now_Y.'-'.$now_M." ";
echo "<a href='?m=".($M-1)."'>前一月</a> ";
echo "<a href='?m=".($M+1)."'>下一月</a> ";
echo "<a href='calmain.php'>转到当前日</a> ";
echo "<table border ='1'>";
echo "<tr>";
echo "<td>星期日</td><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td>";
echo "</tr>";
$cell_index = $index;
for($i = 0; $i< $days ;$i++)
{
if ( ($i+$cell_index) == 0 )
echo "<tr>";
while( $index > 0 )
{
echo "<td> </td>";
$index --;
}
//当前日期
if ( date("Y-m-d",strtotime($now_Y.'-'.$now_M.'-'.($i+1))) == date("Y-m-d") )
echo "<td bgcolor='#FF0000'>".($i+1)."</td>";
else
echo "<td>".($i+1)."</td>";
if ( ($i+$cell_index+1) % 7 == 0 )
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
<html>
<head>
<title>日历程序</title>
</head>
<body>
<?php
/*
//* 根据时间计算每月有多少天
*/
function getDays($month, $year)
{
switch($month)
{
case 4:
case 6:
case 9:
case 11:
$days = 30;
break;
case 2:
if ($year%4==0)
{
if ($year%100==0)
{
$days = $year%400==0 ? 29 : 28;
}
else
{
$days =29;
}
}
else
{
$days = 28;
}
break;
default:
$days = 31;
break;
}
return $days;
}
/*
//* 根据时间戳取得对应的星期几
*/
function getWeek($time_stamp)
{
$week_num = date("w",$time_stamp);
switch($week_num)
{
case "0":
$week = "日";
break;
case '1':
$week = "一";
break;
case '2':
$week = "二";
break;
case '3':
$week = "三";
break;
case '4':
$week = "四";
break;
case '5':
$week = "五";
break;
case '6':
$week = "六";
break;
}
return $week;
}
/*
//* 根据日期相加减
*/
function DateAdd($part, $number, $date)
{
$date_array = getdate(strtotime($date));
$hor = $date_array["hours"];
$min = $date_array["minutes"];
$sec = $date_array["seconds"];
$mon = $date_array["mon"];
$day = $date_array["mday"];
$yar = $date_array["year"];
switch($part)
{
case "y": $yar += $number; break;
case "q": $mon += ($number * 3); break;
case "m": $mon += $number; break;
case "w": $day += ($number * 7); break;
case "d": $day += $number; break;
case "h": $hor += $number; break;
case "n": $min += $number; break;
case "s": $sec += $number; break;
}
return date("Y-m-d H:i:s", mktime($hor, $min, $sec, $mon, $day, $yar));
}
$M = $_GET['m'];
$abs_m = abs($M);
if (is_int($abs_m) and $M <> "" )
{
$date = DateAdd('m', $M, date('Y-m-d H:i:s',strtotime(date('Y').'-'.date('m').'-15')));
$ds = explode('-',$date);
$now_M = $ds[1];
$now_Y = $ds[0];
$days = getDays((int)$now_M, (int)$now_Y);
}
else //默认当前月
{
$now_M = date('m');
$now_Y = date('Y');
$days = getDays((int)$now_M, (int)$now_Y);
}
$index = 0;
switch(getWeek(strtotime($now_Y.'-'.$now_M.'-1 00:00:00')))
{
case '日' : $index = 0;break;
case '一' : $index = 1;break;
case '二' : $index = 2;break;
case '三' : $index = 3;break;
case '四' : $index = 4;break;
case '五' : $index = 5;break;
case '六' : $index = 6;break;
}
echo $now_Y.'-'.$now_M." ";
echo "<a href='?m=".($M-1)."'>前一月</a> ";
echo "<a href='?m=".($M+1)."'>下一月</a> ";
echo "<a href='calmain.php'>转到当前日</a> ";
echo "<table border ='1'>";
echo "<tr>";
echo "<td>星期日</td><td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td>星期六</td>";
echo "</tr>";
$cell_index = $index;
for($i = 0; $i< $days ;$i++)
{
if ( ($i+$cell_index) == 0 )
echo "<tr>";
while( $index > 0 )
{
echo "<td> </td>";
$index --;
}
//当前日期
if ( date("Y-m-d",strtotime($now_Y.'-'.$now_M.'-'.($i+1))) == date("Y-m-d") )
echo "<td bgcolor='#FF0000'>".($i+1)."</td>";
else
echo "<td>".($i+1)."</td>";
if ( ($i+$cell_index+1) % 7 == 0 )
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接利用 php的 date函数 和mktime就行了
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
搞得这么复杂干嘛呢
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
搞得这么复杂干嘛呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
哦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询