I'm trying to subtract 1 month from a date.
$today = date('m-Y');
This gives: 08-2016
How can I subtract a month to get 07-2016
?
I'm trying to subtract 1 month from a date.
$today = date('m-Y');
This gives: 08-2016
How can I subtract a month to get 07-2016
?
Depending on your PHP version you can use DateTime object (introduced in PHP 5.2 if I remember correctly):
You can pass another date to the constructor, it does not have to be the current date. More information: http://php.net/manual/en/datetime.modify.php
Try this,
output
Warning! The above-mentioned examples won't work if call them at the end of a month.
will output:
The following example will produce the same result:
Plenty of ways how to solve the issue can be found in another thread: PHP DateTime::modify adding and subtracting months