php如何求上一个月月初至月末

php如何求上一个月月初至月末,第1张

由于php内置时间函数 strtotime 在求上个月这个功能上存在bug,所以放弃不用了……

上个自己写的临时用的,楼主看看:

$thismonth = date('m');

$thisyear = date('Y');

if($thismonth==1) {

$lastmonth = 12;

$lastyear = $thisyear-1;

} else {

$lastmonth = $thismonth - 1;

$lastyear = $thisyear;

}

$lastStartDay = $lastyear'-'$lastmonth'-1';

$lastEndDay = $lastyear'-'$lastmonth'-'date('t',strtotime($lastStartDay));

echo 'lastStartDay = '$lastStartDay;

echo '<br/>';

echo 'lastEndDay = '$lastEndDay;

$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 )));

}

这里为了演示所以直接把那些相乘计算分开写了,写到程序里时建议直接写结果,减少程序执行时间,这个程序可以封成一个方法,传入一个时间戳就可以获得指定时间的下个月的头天和最后一天了。

以上就是关于php如何求上一个月月初至月末全部的内容,包括:php如何求上一个月月初至月末、php 下个月起始结束日期、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9327669.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存