DateInterval::__construct() - php 日期时间类
DateInterval::__construct()
(PHP 5 >= 5.3.0, PHP 7)
Creates a new DateInterval object
说明
publicDateInterval::__construct(string $interval_spec)Creates a new DateInterval object.
参数
$interval_specAn interval specification.
The format starts with the letterP,forperiod.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 letterT.
Period Designator | Description |
---|---|
Y | years |
M | months |
D | days |
W | weeks. These get converted into days, so can not be combined withD. |
H | hours |
M | minutes |
S | seconds |
Here are some simple examples. Two days isP2D. Two seconds isPT2S. Six years and five minutes isP6YT5M.
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 asP1Y4D,notP4D1Y.
The specification can also be represented as a date time. A sample of one year and four days would beP0001-00-04T00:00:00. But the values in this format can not exceed a given period's roll-over-point (e.g.25hours is invalid).
These formats are based on the» ISO 8601 duration specification.
错误/异常
Throws anExceptionwhen the$interval_speccannot be parsed as an interval.
范例
Example #1DateIntervalexample
以上例程会输出:
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) }
参见
- DateInterval::format() Formats the interval
- DateTime::add() 给一个 DateTime 对象增加一定量的天,月,年,小时,分钟以及秒。
- DateTime::sub() 对一个 DateTime 对象减去一定量的日、月、年、小时、分钟和秒。
- DateTime::diff() Returns the difference between two DateTime objects
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.
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.
I think it is easiest if you would just use the sub method on the DateTime class.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!