Example 1 — Subtract Days, Months, and Hours
Each call changes the same object, so each result builds on the one before it.
<?php
$date = date_create("2024-03-15 09:30:00");
date_sub($date, new DateInterval("P10D"));
echo date_format($date, "Y-m-d H:i"), PHP_EOL;
date_sub($date, new DateInterval("P1M"));
echo date_format($date, "Y-m-d H:i"), PHP_EOL;
date_sub($date, new DateInterval("PT2H"));
echo date_format($date, "Y-m-d H:i"), PHP_EOL; How It Works
- Start at March 15, 2024, 09:30.
- P10D takes away 10 days, which gives March 5.
- P1M takes away one month, which gives February 5.
- PT2H takes away 2 hours, so the time becomes 07:30.

