strtotime() 获取 -1 个月的第 7 天


strtotime() Get 7th day of -1 month

我试过:

$firstOfMonth = "2015-01-01";
$last_month = date("Y-m-d", strtotime('first day of -1 month', strtotime($firstOfMonth)));
                                    /* ^^^^^ This gives me 01 of last month */
/*** Tried 'seventh day of -1 month'
/*   it gives 1970-01-01
*/

我想要的是得到上个月的07。

....一个月的第一天是01....所以基本上你只需要年和月:

$firstOfMonth = "2015-01-01";
$last_month = date("Y-m-01",strtotime("-1 month",strtotime($firstOfMonth)));

就像曼哈顿先生说的,你只需要年份和月份,直到第七天,你可以将其保留为硬编码或变量:

date("Y-m-07",strtotime("-1 month"));
$fixedDay = '07';
echo date("Y-m-$fixedDay",strtotime("-1 month"));