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, 24 Jul 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

설명

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

Adds the specified DateInterval object to the specified DateTime object.

인수

object

절차식 전용: date_create()가 반환하는 DateTime 객체.

interval

The amount to be added. For the date use "P3D", "P3M", "P3Y" or a combination of the three e.g. "P2M5D" (Y = Years, M = Months, D = Days.) MUST BE YEAR MONTH DAY FORMAT "P5Y", "P5M2D", "P5Y4D". For the time use "T3H", "T3M", "T3S" or a combination of the three e.g. "T5H20M" (H = Hours, M = Minutes, S = Seconds). For dateTime use "P5Y2M4DT5H20M". The digit before the letter (NOT P or T) can be any amount.

반환값

Returns the modified DateTime.

예제

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

?>

주의

Warning

이 함수는 실험적입니다. 이 함수의 작동, 함수의 이름, 그리고 관련된 모든 문서는 이후의 PHP 릴리즈에서 예고 없이 변경할 수 있습니다. 이 함수의 사용에 관한 것은 사용자 책임입니다.

참고

  • 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
antonchanning at gmail dot com
23-Sep-2009 09: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, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites