©
本文档使用 PHP中文网手册 发布
(PHP 5 >= 5.3.0, PHP 7)
DateInterval::__construct — Creates a new DateInterval object
$interval_spec
)Creates a new DateInterval object.
interval_spec
An interval specification.
The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.
Period Designator | Description |
---|---|
Y | years |
M | months |
D | days |
W | weeks. These get converted into days, so can not be combined with D. |
H | hours |
M | minutes |
S | seconds |
Here are some simple examples. Two days is P2D. Two seconds is PT2S. Six years and five minutes is P6YT5M.
Note:
The unit types must be entered from the largest scale unit on the left to the smallest scale unit on the right. So years before months, months before days, days before minutes, etc. Thus one year and four days must be represented as P1Y4D, not P4D1Y.
The specification can also be represented as a date time. A sample of one year and four days would be P0001-00-04T00:00:00. But the values in this format can not exceed a given period's roll-over-point (e.g. 25 hours is invalid).
These formats are based on the » ISO 8601 duration specification.
Throws an Exception when the interval_spec
cannot be parsed as an interval.
Example #1 DateInterval example
<?php
$interval = new DateInterval ( 'P2Y4DT6H8M' );
var_dump ( $interval );
?>
以上例程会输出:
object(DateInterval)#1 (8) { ["y"]=> int(2) ["m"]=> int(0) ["d"]=> int(4) ["h"]=> int(6) ["i"]=> int(8) ["s"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) }
[#1] admin at torntech dot com [2015-02-22 21:22:16]
Warning - despite the $interval_spec accepting the ISO 8601 specification format, it does not accept decimal fraction values with period or comma as stated in the specification.
https://bugs.php.net/bug.php?id=53831
<?php
$interval = new DateInterval('P0.5Y');
?>
Will result in
Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (P0.5Y)'
[#2] bkilinc at deyta dot net [2013-12-20 15:28:34]
interval_spec Period Designators, has two 'M's for months and minutes.
[#3] owen at beliefs.com [2013-07-11 15:20:34]
M is used to indicate both months and minutes.
As noted on the referenced wikipedia page for ISO 6801 http://en.wikipedia.org/wiki/Iso8601#Durations
To resolve ambiguity, "P1M" is a one-month duration and "PT1M" is a one-minute duration (note the time designator, T, that precedes the time value).
Using: PHP 5.3.2-1ubuntu4.19
// For 3 Months
$dateTime = new DateTime;echo $dateTime->format( DateTime::ISO8601 ), PHP_EOL;
$dateTime->add(new DateInterval("P3M"));
echo $dateTime->format( DateTime::ISO8601 ), PHP_EOL;
Results in:
2013-07-11T11:12:26-0400
2013-10-11T11:12:26-0400
// For 3 Minutes
$dateTime = new DateTime;echo $dateTime->format( DateTime::ISO8601 ), PHP_EOL;
$dateTime->add(new DateInterval("PT3M"));
echo $dateTime->format( DateTime::ISO8601 ), PHP_EOL;
Results in:
2013-07-11T11:12:42-0400
2013-07-11T11:15:42-0400
Insert a T after the P in the interval to add 3 minutes instead of 3 months.
[#4] buvinghausen at gmail dot com [2012-04-09 18:50:36]
I think it is easiest if you would just use the sub method on the DateTime class.
<?php
$date = new DateTime();
$date->sub(new DateInterval("P89D"));
[#5] daniellehr at gmx dot de [2012-03-21 13:06:36]
Alternatively you can use DateInterval::createFromDateString() for negative intervals:
<?php
$date = new DateTime();
$date->add(DateInterval::createFromDateString('-89 days'));
[#6] jawzx01 at gmail dot com [2012-02-21 22:43:52]
As previously mentioned, to do a negative DateInterval object, you'd code:
<?php
$date1 = new DateTime();
$eightynine_days_ago = new DateInterval( "P89D" );
$eightynine_days_ago->invert = 1; //Make it negative.
$date1->add( $eightynine_days_ago );
?>
and then $date1 is now 89 days in the past.
[#7] kevinpeno at gmail dot com [2011-03-17 11:47:46]
Note that, while a DateInterval object has an $invert property, you cannot supply a negative directly to the constructor similar to specifying a negative in XSD ("-P1Y"). You will get an exception through if you do this.
Instead you need to construct using a positive interval ("P1Y") and the specify the $invert property === 1.
[#8] kuzb [2011-02-04 16:42:39]
It should be noted that this class will not calculate days/hours/minutes/seconds etc given a value in a single denomination of time. For example:
<?php
$di = new DateInterval('PT3600S');
echo $di->format('%H:%i:%s');
?>
will yield 0:0:3600 instead of the expected 1:0:0