如何在PHP中获取上个月或下个月的日期?
我知道日期(“Y”)、日期(“m”)和日期(“d”)将分别返回当前年份(2013)、月份(07)和日期(11) 我正在使用日期格式:“2013-07-11”。我现在有这样的约会。现在我想用PHP获得值“2013-06-11”和“2013-08-11” 获取此值(上个月的同一日期和下个月的同一日期)的代码可能是什么 我试过:如何在PHP中获取上个月或下个月的日期?,php,function,date,get,Php,Function,Date,Get,我知道日期(“Y”)、日期(“m”)和日期(“d”)将分别返回当前年份(2013)、月份(07)和日期(11) 我正在使用日期格式:“2013-07-11”。我现在有这样的约会。现在我想用PHP获得值“2013-06-11”和“2013-08-11” 获取此值(上个月的同一日期和下个月的同一日期)的代码可能是什么 我试过: $LastMonth = date ("m") - 1; $LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d"
$LastMonth = date ("m") - 1;
$LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d");
但这将在10月份返回错误。10月份将显示“2013-010-11”
什么是更好的解决方案?有人能帮我吗?将它与PHP的
strotime()
一起使用:
试试这个
$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
$lst_month=mktime(0,0,0,日期('m')-1,日期('d'),日期('Y'));
回音“M
”。日期(“Y-m-d”,第一个月美元);
$next_month=mktime(0,0,0,日期('m')+1,日期('d'),日期('Y'));
回音“M
”。日期(“Y-m-d”,下月美元);
Hi,为了将来的参考,请记住先快速搜索一下。搜索问题的标题可以得到完美的答案,例如:谢谢!你想在2月和4月为2013-03-31
做什么?@Nirjhor:如果你觉得这个答案有帮助,请随意接受。:-)
$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
$date = new DateTime( "2013-07-11");
$date->modify("+1 month");
echo $date->format(‘l, F jS, Y’);
$nextmonth=date("dmy",strtotime("+1 month"));
$lastmonth=date("dmy",strtotime("-1 month"));