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

search for in the

DateTime::__construct> <DateTime
[edit] Last updated: Fri, 17 May 2013

view this page in

DateTime::add

date_add

(PHP 5 >= 5.3.0)

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

Beschreibung

Objektorientierter Stil

public DateTime DateTime::add ( DateInterval $interval )

Prozeduraler Stil

DateTime date_add ( DateTime $object , DateInterval $interval )

Adds the specified DateInterval object to the specified DateTime object.

Parameter-Liste

object

Nur bei prozeduralem Aufruf: Ein von date_create() zurückgegebenes DateTime-Objekt. Diese Funktion verändert dieses Objekt.

interval

A DateInterval object

Rückgabewerte

Gibt das DateTime-Objekt für die Verkettung von Methoden zurück Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 DateTime::add() example

Objektorientierter Stil

<?php
$date 
= new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo 
$date->format('Y-m-d') . "\n";
?>

Prozeduraler Stil

<?php
$date 
date_create('2000-01-01');
date_add($datedate_interval_create_from_date_string('10 days'));
echo 
date_format($date'Y-m-d');
?>

Die obigen Bespiele erzeugen folgende Ausgabe:

2000-01-11

Beispiel #2 Further DateTime::add() examples

<?php
$date 
= new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo 
$date->format('Y-m-d H:i:s') . "\n";

$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo 
$date->format('Y-m-d H:i:s') . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

2000-01-01 10:00:30
2007-06-05 04:03:02

Beispiel #3 Beware when adding months

<?php
$date 
= new DateTime('2000-12-31');
$interval = new DateInterval('P1M');

$date->add($interval);
echo 
$date->format('Y-m-d') . "\n";

$date->add($interval);
echo 
$date->format('Y-m-d') . "\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

2001-01-31
2001-03-03

Anmerkungen

DateTime::modify() is an alternative when using PHP 5.2.

Siehe auch



add a note add a note User Contributed Notes DateTime::add - [2 notes]
up
3
fortruth at mabang dot net
2 years ago
adding 15 min to a datetime

<?php
$initDate
= new DateTime("2010/08/24");

$initDate->add(new DateInterval("PT15M"));
echo
$initDate->format("Y/m/d m:i:s");//result: 2010/08/24 08:15:00
?>

period:
P1Y2M3DT1H2M3S

period time:
PT1H2M3S
up
5
Anonymous
2 years ago
Note that the add() and sub() methods will modify the value of the object you're calling the method on! This is very untypical for a method that returns a value of its own type. You could misunderstand it that the method would return a new instance with the modified value, but in fact it modifies itself! This is undocumented here. (Only a side note on procedural style mentions it, but it obviously does not apply to object oriented style.)

 
show source | credits | stats | sitemap | contact | advertising | mirror sites