大网数据广告活动
PHP 语言与运行手册

DatePeriod::createFromISO8601String · Creates a new DatePeriod object from an ISO8601 string

· 52CE 我爱测

DatePeriod::createFromISO8601String — Creates a new DatePeriod object from an ISO8601 string

DatePeriod::createFromISO8601String

(PHP 8 >= 8.3.0)

DatePeriod::createFromISO8601String — Creates a new DatePeriod object from an ISO8601 string

Description

public static function DatePeriod::createFromISO8601String(string $specification, int $options = 0): static

Creates a new DatePeriod object from an ISO8601 string, as specified with specification.

Parameters

specification

A subset of the » ISO 8601 repeating interval specification.

An example of an accepted ISO 8601 interval specifications is R5/2008-03-01T13:00:00Z/P1Y2M10DT2H30M, which specifies:

  • 5 iterations (R5/)

  • Starting at 2008-03-01T13:00:00Z.

  • Each iteration is an 1 year, 2 months, 10 days, 2 hours, and 30 minute interval (/P1Y2M10DT2H30M).

Examples of some ISO 8601 interval specification features that PHP does not support are:

  1. zero occurrences (R0/)

  2. time offsets other than UTC (Z), such as +02:00.

options

A bit field which can be used to control certain behaviour with start- and end- dates.

With DatePeriod::EXCLUDE_START_DATE you exclude the start date from the set of recurring dates within the period.

With DatePeriod::INCLUDE_END_DATE you include the end date in the set of recurring dates within the period.

Return Values

Creates a new DatePeriod object.

DatePeriod objects created with this method can be used as an iterator to generate a number of DateTimeImmutable objects.

Errors/Exceptions

Throws an DateMalformedPeriodStringException when the specification cannot be parsed as a valid ISO 8601 period.

Examples

Example #1 DatePeriod::createFromISO8601String example

<?php
$iso = 'R4/2023-07-01T00:00:00Z/P7D';
$period = DatePeriod::createFromISO8601String($iso);

// By iterating over the DatePeriod object, all of the
// recurring dates within that period are printed.
foreach ($period as $date) {
    echo $date->format('Y-m-d'), "\n";
}

The above example will output:

2023-07-01
2023-07-08
2023-07-15
2023-07-22
2023-07-29

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。