How to subtract days from a date using Carbon in PHP?

To subtract days from a date using Carbon in PHP, you can use the subDays() method. Here is an example:

use Carbon\Carbon; $startDate = Carbon::parse('2022-06-01'); $daysToSubtract = 5; $endDate = $startDate->subDays($daysToSubtract); echo $endDate; // Output: 2022-05-27

In this example, we start with a Carbon instance for the $startDate, which is set to June 1, 2022. We then specify the number of days to subtract using the $daysToSubtract variable.

Next, we call the subDays() method on the $startDate instance, passing in the $daysToSubtract variable as an argument. This subtracts the specified number of days from the date.

Finally, we can output the result using the echo statement. In this case, the output will be May 27, 2022, as we subtracted 5 days from June 1, 2022.