PHP Function Reference

PHP DateTimeImmutable - add() Method



The PHP DateTimeImmutable::add() method adds the specified interval (days, months, years, hours, minutes and seconds) to the given DateTimeImmutable object.

Syntax

public DateTimeImmutable::add(interval)

Parameters

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

Return Value

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

Example: Adding Days, Months & Years

Consider the example below which explains how to add 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";

  //adding 1 year 2 months 10 days
  $date_new = $date->add($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: 24-Jul-2016

Example: Adding Hours, Minutes and Seconds

The example below illustrates how to add 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";

  //adding 5 hours 30 minutes 45 seconds
  $date_new = $date->add($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: 14-May-2015 05:30:45

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

The example below illustrates how to add 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";

  //adding 1 year 2 months 10 days 5 hours 30 minutes 45 seconds
  $date_new = $date->add($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: 24-Jul-2016 05:30:45

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";

  //adding 1 year 100 days 
  $date_new = $date->add($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: 22-Aug-2016

Example: Adding Months

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

<?php
  //datetime string
  $datetime_string = "31-Jan-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";

  //adding 1 month
  $date_new = $date->add($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-Jan-2015
Modified DateTime: 03-Mar-2015

❮ PHP Date and Time Reference