php 下个月起始结束日期
答案:4 悬赏:0 手机版
解决时间 2021-02-26 21:05
- 提问者网友:萌卜娃娃
- 2021-02-25 22:17
以今天为例,2015.01.30。代码里获取下个月起始和结束日期20150201 和20150228,代码里怎样写。是否只能加判断?直接date('Ym01', strtotime('+1 month'));出来的不对
最佳答案
- 五星知识达人网友:有你哪都是故乡
- 2021-02-25 22:32
<?php
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));
}
$today = "2015-01-30";
$nowMonth = date("m", strtotime($today));
$nowYear = date("Y", strtotime($today));
$nextMonth1 = DateAdd('m', 1, $nowYear."-".$nowMonth."-1");
$nextnextMonth = DateAdd('m', 2, $nowYear."-".$nowMonth."-1");
$nextMonth2 = DateAdd('d', -1, $nextnextMonth );
echo "下月开始日期:".date("Ymd",strtotime($nextMonth1));
echo "<br>";
echo "下月结束日期:".date("Ymd",strtotime($nextMonth2));
exit();
?>
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));
}
$today = "2015-01-30";
$nowMonth = date("m", strtotime($today));
$nowYear = date("Y", strtotime($today));
$nextMonth1 = DateAdd('m', 1, $nowYear."-".$nowMonth."-1");
$nextnextMonth = DateAdd('m', 2, $nowYear."-".$nowMonth."-1");
$nextMonth2 = DateAdd('d', -1, $nextnextMonth );
echo "下月开始日期:".date("Ymd",strtotime($nextMonth1));
echo "<br>";
echo "下月结束日期:".date("Ymd",strtotime($nextMonth2));
exit();
?>
全部回答
- 1楼网友:西风乍起
- 2021-02-26 01:20
function getdays($day){
$firstday = date('Y-m-d', strtotime(date('Y-m-01', strtotime($day)) . ' +1 month'));//每个月的开始日期肯定是1号
$lastday = date('Y-m-d', strtotime(date('Y-m-01', strtotime($day)) . ' +2 month -1 day'));
return array($firstday,$lastday);
}
print_r(getdays('2015-01-30'));
- 2楼网友:狂恋
- 2021-02-26 01:02
协议已经生效,但是起始日期是下个月,所以银行会在下个月扣款,所以网上现在没有你定投的信息。
- 3楼网友:不如潦草
- 2021-02-25 23:57
$now = time();
$now_m = date("m", $now);
$next_line = $now + 28 * 60 * 60 * 24 - 1;
if(date("m", $next_line ) == $now_m ){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line )));
$last = date("Ymd", strtotime(date("Y-m-28", $next_line )));
}else if(date("m", $next_line + 60 * 60 * 24 ) == $now_m){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line + 60 * 60 * 24 )));
$last = date("Ymd", strtotime(date("Y-m-29", $next_line + 60 * 60 * 24 )));
}else if(date("m", $next_line + 60 * 60 * 24 * 2 ) == $now_m){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line + 60 * 60 * 24 * 2 )));
$last = date("Ymd", strtotime(date("Y-m-30", $next_line + 60 * 60 * 24 * 2 )));
}else if(date("m", $next_line + 60 * 60 * 24 * 3 ) == $now_m){
$first = date("Ymd", strtotime(date("Y-m-1", $next_line + 60 * 60 * 24 * 3 )));
$last = date("Ymd", strtotime(date("Y-m-31", $next_line + 60 * 60 * 24 * 3 )));
}
这里为了演示所以直接把那些相乘计算分开写了,写到程序里时建议直接写结果,减少程序执行时间,这个程序可以封成一个方法,传入一个时间戳就可以获得指定时间的下个月的头天和最后一天了。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯