我想使用 PHP 提取当前月份(即 3 月(最后一个周末的开始日期时间?
Example:
Last weekend of March this year would be: 23-03-2018.
要获得最后一个完整的周末(我阅读了这个问题,因为这是您想要的(,您需要找到每月的最后一天并查看那一天。
我使用 date("w"( 来获取星期日的日期编号 0 和星期六的日期编号 6。
如果数字为 5 或更大,则当月不会发生整个周末。
如果是这种情况,我会从 Unix 时间中删除几天并找到上一个星期五。
$month = "March";
If(date("w", strtotime("last day of ". $month)) >= 5){
$unix = strtotime("last day of ". $month)-86400*6;
Echo date("Y-m-d", strtotime("previous Friday " .date("Y-m-d", $unix)));
}Else{
Echo date("Y-m-d", strtotime("previous Friday " .date("Y-m-d",strtotime("last day of ". $month))));
}
在这里试试: https://3v4l.org/O4TDd
我认为你应该找到一个解决方案 strtotime
您可以迭代数月
strtotime("-1 months"));
strtotime("1 months"));
然后获取最后一周结束日期
strtotime("last saturday");
strtotime("last sunday");
尝试:
echo strtotime("+[your variable]"+ months last sunday");
其中 [您的变量] 是当前月份之前或之后的月数
编辑:
在评论中看到@ChrisG答案后,最好的解决方案实际上是:
$month = [your month variable];
echo strtotime("last saturday of "+ $month);