0

If I have a date that equals: 2020-04-21

and I want to take the day of this date: 21

take the current month: December

calculate it as if it were December then: 2020-12-21

and transform it into the previous month, keeping day and year, then: 2020-11-21

how can I do?

1 Answers1

0
<?
$time=strtotime('2020-04-21'); // get unixtime of custom date
$day=date('d', $time); // get day of custom date
$year=date('Y', $time); // get year of custom date
$month=date('m'); // get current month

$time_first=mktime(0, 0, 0, $month, $day, $year); // get unixtime for first
$time_second=strtotime('-1 month', $time_first); // get unixtime previous month

$date_first=date('Y-m-d', $time_first); // string format of first date
$date_second=date('Y-m-d', $time_second); // string format of second date

var_dump($date_first);
var_dump($date_second);
?>
miniman
  • 61
  • 3