PHP Function Reference

PHP DateTimeImmutable - sub() Method



The PHP DateTimeImmutable::sub() method subtracts the specified interval (days, months, years, hours, minutes and seconds) from the given DateTimeImmutable object.

Syntax

public DateTimeImmutable::sub(interval)

Parameters

interval Required. A DateInterval object specifying the interval to be subtracted.

Return Value

Returns a new DateTimeImmutable object with subtracted interval on success, otherwise returns false.

Example: Subtracting Days, Months & Years

Consider the example below which explains how to subtract days, months and years to a given DateTimeImmutable object.

<?php
  //datetime string
  $datetime_string = "14-May-2015";

  //creating a DateTimeImmutable object
  $date = new DateTimeImmutable($datetime_string);

  //creating a DateInterval object
  $interval = new DateInterval('P1Y2M10D'); 

  //formatting the datetime to print it
  echo "Original date: ".$date->format("d-M-Y")."\n";

  //subtracting 1 year 2 months 10 days
  $date_new = $date->sub($interval); 

  //formatting the datetime to print it
  echo "Modified date: ".$date_new->format("d-M-Y")."\n";
?>

The output of the above code will be:

Original date: 14-May-2015
Modified date: 04-Mar-2014

Example: Subtracting Hours, Minutes and Seconds

The example below illustrates how to subtract hours, minutes and seconds to a given DateTimeImmutable object.

<?php
  //datetime string
  $datetime_string = "14-May-2015";

  //creating a DateTimeImmutable object
  $date = new DateTimeImmutable($datetime_string);

  //creating a DateInterval object
  $interval = new DateInterval('PT5H30M45S'); 

  //formatting the datetime to print it
  echo "Original DateTime: ".$date->format("d-M-Y H:i:s")."\n";

  //subtracting 5 hours 30 minutes 45 seconds
  $date_new = $date->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date_new->format("d-M-Y H:i:s")."\n";
?>

The output of the above code will be:

Original DateTime: 14-May-2015 00:00:00
Modified DateTime: 13-May-2015 18:29:15

Example: Subtracting Days, Months, Years, Hours, Minutes and Seconds

The example below illustrates how to subtract days, months, years, hours, minutes and seconds to a given DateTimeImmutable object.

<?php
  //datetime string
  $datetime_string = "14-May-2015";

  //creating a DateTimeImmutable object
  $date = new DateTimeImmutable($datetime_string);

  //creating a DateInterval object
  $interval = new DateInterval('P1Y2M10DT5H30M45S'); 

  //formatting the datetime to print it
  echo "Original DateTime: ".$date->format("d-M-Y H:i:s")."\n";

  //subtracting 1 year 2 months 10 days 5 hours 30 minutes 45 seconds
  $date_new = $date->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date_new->format("d-M-Y H:i:s")."\n";
?>

The output of the above code will be:

Original DateTime: 14-May-2015 00:00:00
Modified DateTime: 03-Mar-2014 18:29:15

Example: Creating DateInterval object from Date String

A DateInterval object can be created using date_interval_create_from_date_string() method. Consider the example below:

<?php
  //datetime string
  $datetime_string = "14-May-2015";

  //creating a DateTimeImmutable object
  $date = new DateTimeImmutable($datetime_string);

  //creating a DateInterval object
  $interval = date_interval_create_from_date_string('1 year 100 days'); 

  //formatting the datetime to print it
  echo "Original DateTime: ".$date->format("d-M-Y")."\n";

  //subtracting 1 year 100 days 
  $date_new = $date->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date_new->format("d-M-Y")."\n";
?>

The output of the above code will be:

Original DateTime: 14-May-2015
Modified DateTime: 03-Feb-2014

Example: Subtracting Months

Be cautious, when subtracting months. Consider the example below:

<?php
  //datetime string
  $datetime_string = "31-Mar-2015";

  //creating a DateTimeImmutable object
  $date = new DateTimeImmutable($datetime_string);

  //creating a DateInterval object
  $interval = new DateInterval('P1M'); 

  //formatting the datetime to print it
  echo "Original DateTime: ".$date->format("d-M-Y")."\n";

  //subtracting 1 month
  $date_new = $date->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date_new->format("d-M-Y")."\n";
?>

The output of the above code will be:

Original DateTime: 31-Mar-2015
Modified DateTime: 03-Mar-2015

❮ PHP Date and Time Reference