PHP 8.3.4 Released!

DatePeriod クラス

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

はじめに

日付の期間をあらわします。

日付の期間を使うと、指定した期間に定期的に発生する内容を反復処理できます。

クラス概要

class DatePeriod implements IteratorAggregate {
/* 定数 */
public const int EXCLUDE_START_DATE;
public const int INCLUDE_END_DATE;
/* プロパティ */
public readonly ?DateTimeInterface $start;
public readonly ?DateTimeInterface $current;
public readonly ?DateTimeInterface $end;
public readonly ?DateInterval $interval;
public readonly int $recurrences;
public readonly bool $include_start_date;
public readonly bool $include_end_date;
/* メソッド */
public __construct(
    DateTimeInterface $start,
    DateInterval $interval,
    int $recurrences,
    int $options = 0
)
public __construct(
    DateTimeInterface $start,
    DateInterval $interval,
    DateTimeInterface $end,
    int $options = 0
)
public __construct(string $isostr, int $options = 0)
public static createFromISO8601String(string $specification, int $options = 0): static
}

定義済み定数

DatePeriod::EXCLUDE_START_DATE

開始日を含まない。DatePeriod::__construct() で使用します。

DatePeriod::INCLUDE_END_DATE

終了日を含む。DatePeriod::__construct() で使用します。

プロパティ

recurrences

イテレータが返す最小のインスタンスの数。

DatePeriod のコンストラクタの recurrences に繰り返しの回数を明示的に渡した場合、 このプロパティにはその値が含まれます。 DatePeriod::EXCLUDE_START_DATE によって開始日が無効にされていない場合、 このプロパティには それよりひとつ多い 値が設定されます。 DatePeriod::INCLUDE_END_DATE で終了日が有効になっている場合、 このプロパティには さらにひとつ多い 値が設定されます。

繰り返しの回数を明示的に渡さなかった場合、 このプロパティには返される最小のインスタンスの数が含まれます。 この値は 0 になるはずですが、 DatePeriod::EXCLUDE_START_DATE によって開始日が無効にされていない場合、 このプロパティには それよりひとつ多い 値が設定されます。 DatePeriod::INCLUDE_END_DATE で終了日が有効になっている場合、 このプロパティには さらにひとつ多い 値が設定されます。

<?php
$start
= new DateTime('2018-12-31 00:00:00');
$end = new DateTime('2021-12-31 00:00:00');
$interval = new DateInterval('P1M');
$recurrences = 5;
// コンストラクタで明示的に繰り返し回数を設定
$period = new DatePeriod($start, $interval, $recurrences, DatePeriod::EXCLUDE_START_DATE);
echo
$period->recurrences, "\n";
$period = new DatePeriod($start, $interval, $recurrences);
echo
$period->recurrences, "\n";
$period = new DatePeriod($start, $interval, $recurrences, DatePeriod::INCLUDE_END_DATE);
echo
$period->recurrences, "\n";
// コンストラクタで繰り返し回数を設定しない場合
$period = new DatePeriod($start, $interval, $end);
echo
$period->recurrences, "\n";
$period = new DatePeriod($start, $interval, $end, DatePeriod::EXCLUDE_START_DATE);
echo
$period->recurrences, "\n";
?>

上の例の出力は以下となります。


5
6
7
1
0

DatePeriod::getRecurrences() もご覧ください。

include_end_date

終了日を反復日のセットに含めるかどうか。

include_start_date

開始日を反復日のセットに含めるかどうか。

start

期間の開始日。

current

反復中に期間内の現在の日付が含まれます。

end

期間の終了日。

interval

ISO 8601 の繰り返し間隔の指定。

変更履歴

バージョン 説明
8.2.0 定数 DatePeriod::INCLUDE_END_DATE と プロパティ include_end_date が追加されました。
8.0.0 DatePeriod は、 IteratorAggregate を実装するようになりました。 これより前のバージョンでは、 Traversable を実装していました。

目次

add a note

User Contributed Notes 1 note

up
20
mail at pascalhofmann dot de
7 years ago
When looping over a DatePeriod object, the returned objects always implement DateTimeInterface. The exact type returned depends on how the DatePeriod was created. If $start was a DateTimeImmutable, the objects returned will be of type DateTimeImmutable. If a DateTime object was used, the objects returned will be of type DateTime.
To Top