Hi everyone! I'm the author of code.mu :)
I'd love to chat with my non-Russian audience. I'm looking for your feedback on the site and the translation quality. Let's chat:)
238 of 410 menu
◀ ▶

The sub Method of the DateTime Class

The sub method of the DateTime class subtracts the specified time interval from the date. It accepts an object of DateInterval as a parameter. The date object itself is modified in the process.

Syntax

$datetime->sub(DateInterval $interval);

Example

Subtract 2 days from the current date:

<?php $date = new DateTime('2023-05-15'); $interval = new DateInterval('P2D'); $date->sub($interval); echo $date->format('Y-m-d'); ?>

Code execution result:

'2023-05-13'

Example

Subtract 1 month and 5 days from the specified date:

<?php $date = new DateTime('2023-06-20'); $interval = new DateInterval('P1M5D'); $date->sub($interval); echo $date->format('Y-m-d'); ?>

Code execution result:

'2023-05-15'

See Also

  • the date_add function,
    which adds an interval to a date
  • the date_diff function,
    which calculates the difference between dates
← →
↑
English▼
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline