百科狗-知识改变命运!
--

DateInterval::__construct() - php 日期时间类

乐乐1年前 (2023-11-21)阅读数 14#技术干货
文章标签定量

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_spec

DateInterval::__construct() - php 日期时间类

An 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.

$interval_specPeriod Designators
Period DesignatorDescription
Yyears
Mmonths
Ddays
Wweeks. These get converted into days, so can not be combined withD.
Hhours
Mminutes
Sseconds

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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)