downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DateTime::__construct> <DateTime
Last updated: Fri, 11 Dec 2009

view this page in

DateTime::add

(PHP 5 >= 5.3.0)

DateTime::add Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object

Description

public DateTime DateTime::add ( DateInterval $interval )
DateTime date_add ( DateTime $object , DateInterval $interval )

Adds the specified DateInterval object to the specified DateTime object.

Parameters

object

Procedural style only: A DateTime object returned by date_create()

interval

A DateInterval object

Return Values

Returns the modified DateTime.

Examples

Example #1 date_add() example

<?php

$date 
= new DateTime("18-July-2008 16:30:30");
echo 
$date->format("d-m-Y H:i:s").'<br />';

date_add($date, new DateInterval("P5D"));
echo 
'<br />'.$date->format("d-m-Y").' : 5 Days';

date_add($date, new DateInterval("P5M"));
echo 
'<br />'.$date->format("d-m-Y").' : 5 Months';

date_add($date, new DateInterval("P5Y"));
echo 
'<br />'.$date->format("d-m-Y").' : 5 Years';

date_add($date, new DateInterval("P5Y5M5D"));
echo 
'<br />'.$date->format("d-m-Y").' : 5 Days, 5 Months, 5 Years';

date_add($date, new DateInterval("P5YT5H"));
echo 
'<br />'.$date->format("d-m-Y H:i:s").' : 5 Years, 5 Hours';

?>

See Also

  • DateTime::sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
  • DateTime::diff() - Returns the difference between two DateTime objects



add a note add a note User Contributed Notes
DateTime::add
kb at imodus dot pl
07-Dec-2009 09:56
As noted in DateTime::modify comment The P6M sometimes gives nonintuitive results, especially for last day of the month: 2009-05-31 + 1 month = 2009-06-31 => 2009-07-01;

To get expected behavior convert the date to first day of month , modify date, than use 't' for day in format to get last day of the month.

Example:

<?php
        $f
= 'Y-m-d H:i:s';
       
$f1 = 'Y-m-01 H:i:s';
       
$fL = 'Y-m-t H:i:s';
       
       
//EXPECTED
       
$lastDayOfTheMonth  = new DateTime('2009-12-31 00:00:00');
       
$firstDayOfTheMonth = new DateTime($lastDayOfTheMonth->format($f1));
       
//echoes 2009-06-30
       
echo $lastDayNext6Months = $firstDayOfTheMonth->add(new DateInterval("P6M"))->format($fL),'<br />';

       
//NON-EXPECTED
       
$lastDayOfTheMonth  = new DateTime('2009-12-31 00:00:00');
       
//echoes 2009-07-01
       
echo $lastDayNext6Months = $lastDayOfTheMonth->add(new DateInterval("P6M"))->format($f),'<br />';
       

?>
antonchanning at gmail dot com
23-Sep-2009 12:32
If you are currently using php 5.2.x (The latest version in the Ubuntu repository is 5.2.4 as I write this), and need to add to a date, consider whether using DateTime::modify will work for you, as DateTime::add is only available from 5.3

DateTime::__construct> <DateTime
Last updated: Fri, 11 Dec 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites