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, 13 Nov 2009

view this page in

DateTime::add

(PHP 5 >= 5.3.0)

DateTime::add 年月日時分秒の値を DateTime オブジェクトに加える

説明

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

指定した DateInterval オブジェクトを、 指定した DateTime オブジェクトに加えます。

パラメータ

object

手続き型のみ: date_create() が返す DateTime オブジェクト

interval

DateInterval オブジェクト。

返り値

変更した DateTime を返します。

例1 date_add() の例

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

?>

参考

  • DateTime::sub() - 年月日時分秒の値を DateTime オブジェクトから引く
  • DateTime::diff() - ふたつの DateTime オブジェクトの差を返す



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, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites