PHP subtract 1 month from date formatted with date ('m-Y')
You can use the DateTime object in PHP to subtract one month from a date formatted with date('m-Y').
You can use the date() and strtotime() functions in PHP to subtract one month from a date formatted with date('m-Y'). Here is an example:
Example of using PHP date function to subtract one month from a date formatted with date('m-Y')
<?php
$date = '2023-05-15';
$prev_month = date('m-Y', strtotime("-1 month", strtotime($date)));
echo "Current month: " . date('m-Y', strtotime($date)) . "\n";
echo "Previous month: " . $prev_month . "\n";
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
This will output 05-2023 and 04-2023. The strtotime() function parses the relative time string "-1 month" against the base date, and the date() function formats the resulting timestamp into the m-Y format.