PHP | date_add() Function

PHP date_add() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP date_add() Function.

The PHP date_add() function is an alias of DateTime::add(). This PHP function accepts DateTime and DateInterval object as parameters, adds the specified intetval to the given DateTime.

Syntax

Following below is the syntax to use this function -

date_add($object, $interval)


Parameter Details

Sr.NoParameter & Description
1

object(Optional)

This is a DateTime object specifying/representing the date to which you need to add the time interval.

2

interval (Optional)

This is a DateInterval object specifying the interval to be added.


Return Value

This function returns a DateTime object with added interval. In case of failure it returns false.

PHP Version

This function was first introduced as part of core PHP version 5.3.0 and, it works with all of the later versions.

Example1

Following example illustrates the usage of date_add() function -

<?php
   //Creating a DateTime object
   $date = date_create("25-09-1989");
   //Adding interval to the date
   $res = date_add($date, new DateInterval('PT10H30S'));   
   //formatting the date to print it
   $format = date_format( $res, "d-m-Y H:i:s");
   print($format);
?>

Output

When the above code is executed, it will produce the following result -

25-09-1989 10:00:30

Example2

You can create an interval with the date_interval_create_from_date_string() function. The following creates an interval using this function, adds it to a date -

<?php
   $date = date_create("25-09-1989");
   $interval = date_interval_create_from_date_string('1025 days');
   $res = date_add($date, $interval);   
   $format = date_format( $res, "d-m-Y H:i:s");
   print($format);   
?>

Output

When the above code is executed, it will produce the following result -

16-07-1992 00:00:00

Example3

Now let us try to add interval with years, months, and days -

<?php
   //Creating a DateTime object
   $date = date_create("25-09-1989");
   //Adding interval to the date
   $res = date_add($date, new DateInterval('P29Y2M5D'));   
   //formatting the date to print it
   $format = date_format( $res, "d-m-Y");
   print($format);
?>

Output

When the above code is executed, it will produce the following result -

30-11-2018

Example4

Try the following example -

<?php
   $date = date_create('1995-05-07');
   $interval = date_interval_create_from_date_string('150 days');
   $date->add($interval);
   print($date -> format('d-m-Y'));
?>

Output

When the above code is executed, it will produce the following result -

04-10-1995


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_create_from_format() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.