PHP Function Reference

PHP DateTime - sub() Method



The PHP DateTime::sub() method subtracts the specified interval (days, months, years, hours, minutes and seconds) from the given DateTime object. The date_sub() function is an alias of this method.

Syntax

//Object-oriented style
public DateTime::sub(interval)

//Procedural style
date_sub(object, interval)

Parameters

object Required. For procedural style only: A DateTime object returned by date_create().
interval Required. A DateInterval object specifying the interval to be subtracted.

Return Value

Returns the DateTime object with subtracted interval on success, otherwise returns false.

Example: using both styles

The example below shows the usage of DateTime::sub() method.

<?php
  //datetime string
  $datetime_string = "24-Dec-2015";

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

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

  //subtracting interval using Object-oriented style
  $date->sub($interval); 

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

  //subtracting interval using Procedural style
  date_sub($date, $interval); 

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

The output of the above code will be:

19-Dec-2015
14-Dec-2015

Example: Subtracting Days, Months & Years

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

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

  //creating a DateTime object
  $date = new DateTime($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->sub($interval); 

  //formatting the datetime to print it
  echo "Modified date: ".$date->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 DateTime object.

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

  //creating a DateTime object
  $date = new DateTime($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->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date->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 DateTime object.

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

  //creating a DateTime object
  $date = new DateTime($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->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date->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 DateTime object
  $date = new DateTime($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->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date->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 DateTime object
  $date = new DateTime($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->sub($interval); 

  //formatting the datetime to print it
  echo "Modified DateTime: ".$date->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