我有点不确定为什么这不能找到上个月的最后一天.每个步骤似乎都能正常工作,除非最终日期被创建.
<?PHP
$currentMonth = date('n');
$currentYear = date('Y');
if($currentMonth == 1) {
$lastMonth = 12;
$lastYear = $currentYear - 1;
}
else {
$lastMonth = $currentMonth -1;
$lastYear = $currentYear;
}
if($lastMonth < 10) {
$lastMonth = '0' . $lastMonth;
}
$lastDayOfMonth = date('t',$lastMonth);
$lastDateOfPrevIoUsMonth = $lastYear . '-' . $lastMonth . '-' . $lastDayOfMonth;
$newLastDateOfMonth = date('F j,Y',strtotime($lastDateOfPrevIoUsMonth));
?>
$lastDateOfPrevIoUsMonth正如预期返回2012-09-30;然而,在尝试将其转换为2012年9月30日之后 – $newLastDateOfMonth将于2012年10月1日返回.我似乎在哪里出错?
编辑:如果使用日期(“t / m / Y”,strtotime(“上个月”));或日期(‘Y-m-d’,strtotime(“上个月的最后一天”));在2013-01-01期间,这两个项目中哪一项仍然可行,那么它们会在一年中变现吗?
echo date('Y-m-d',strtotime('last day of prevIoUs month'));
//2012-09-30
要么
$date = new DateTime();
$date->modify("last day of prevIoUs month");
echo $date->format("Y-m-d");
后来编辑:php.net documentation – relative formats for strtotime(),DateTime and date_create()