Warning! The above examples will not work if you call them at the end of the month.
<?php $now = mktime(0, 0, 0, 10, 31, 2017); echo date("mY", $now)."\n"; echo date("mY", strtotime("-1 months", $now))."\n";
will output:
10-2017 10-2017
The following example will give the same result:
$date = new DateTime('2017-10-31 00:00:00'); echo $date->format('m-Y')."\n"; $date->modify('-1 month'); echo $date->format('m-Y')."\n";
Many ways to solve the problem can be found in another thread: PHP DateTime :: change the time of adding and subtracting
source share