php “P”在 DateInterval 格式中代表什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9188763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:19:44  来源:igfitidea点击:

What does 'P' stand for in the DateInterval format?

phpphp-5.3

提问by Shrinath

Consider the following example quoted from php manual for DateTime

考虑以下从php 手册中引用的 DateTime示例

<?php
  $date = new DateTime('2000-01-20');
  $date->sub(new DateInterval('P10D'));
  echo $date->format('Y-m-d') . "\n";
?>

'D' is for days, what does the 'P' stand for in that formatting?

'D' 代表几天,'P' 在那种格式中代表什么?

回答by Phil

From the manual

手册

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.

间隔规范。

格式以字母 P 开头,表示“句点”。每个持续时间段由一个整数值表示,后跟一个时间段指示符。如果持续时间包含时间元素,则规范的该部分前面有字母 T。

回答by Java

回答by unclexo

I think it can be answered in more details. First of all, DateIntervalconstructor method takes one parameter named $interval_specwhich is string.

我认为可以更详细地回答。首先,DateInterval构造函数方法接受一个名为$interval_spec字符串的参数。

DateInterval::__construct ( string $interval_spec )

This parameter has a specification described as below:

该参数的规格如下:

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.

格式以字母 P 开头,表示句点。每个持续时间段由一个整数值表示,后跟一个时间段指示符。如果持续时间包含时间元素,则规范的该部分前面有字母 T。

There are some Period Designators that are used in the argument:

参数中使用了一些句号:

  • Yfor years
  • Mfor months
  • Dfor days
  • Wfor weeks. These get converted into days, so can not be combined with D.
  • Hfor hours
  • Mfor minutes
  • Sfor seconds
  • Ÿ
  • 中号
  • ð
  • W。这些被转换成天,所以不能与 D 结合。
  • ^ h小时
  • 中号
  • 小号

Let's see some example using Period Designators:

让我们看一些使用句号指示符的示例:

  • Two days is P2D.
  • Two seconds is PT2S.
  • Six years and five minutes is P6YT5M.
  • 两天是P2D。
  • 两秒是PT2S。
  • 六年零五分钟是P6YT5M。

There is an order that needs to be maintained as described the doc:

有一个订单需要按照文档中的描述进行维护:

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.

单位类型必须从左侧的最大比例单位到右侧的最小比例单位输入。所以几年前几个月,几个月前几天,几天前几分钟,等等。因此一年零四天必须表示为 P1Y4D,而不是 P4D1Y。

The specification can also be represented as a datetime.

该规范也可以表示为 datetime

  • One year, two months, four days would be P0001-02-04T00:00:00
  • 一年两个月四天就是P0001-02-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).

但是这种格式的值不能超过给定时间段的滚动点(例如 25 小时无效)。