我想把这个月的第一天(Y-M-01)设置为day> 1。但如果这一天已经是这个月的第一天,它应该给我"-1 month"。同样的事情也应该能够在未来发生。(+ 1个月)
我如何在php中管理这个?
(更新)得到了这个代码工作的倒退,但我不能使用相同的方法向前作为一个月的最后一天可能是28,29,30或31。应该用月的最后一天,但是我不知道怎么用。
if (date("j", strtotime('today', $time)) == 1) {
$beginmon = strtotime('first day of last month', $time);
} else {
$beginmon = strtotime('first day of this month', $time);
}
听起来很简单
<?php
if(date("j")>1)
{
echo date("Y-m-01");
}
else
{
echo date("Y-m-d",strtotime("-1 month"));
}
?>
编辑
可以根据您的环境修改为
<?php
if(date("j")==1)
{
$beginmon = strtotime(date("Y-m-01"));
}
else
{
$beginmon=strtotime("-1 month");
}
?>
$date = strtotime('first day of this month', time());
if (date("Y M dd", $date) == date("Y M dd", time())){
$date = strtotime('first day of last month', time());
}