PHP 8.3.4 Released!

DateInterval クラス

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

はじめに

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

格納方式は、固定値 (年、月、日、時など) あるいは相対時刻 (DateTimeImmutableDateTime のコンストラクタがサポートしている書式) となります。

さらに厳密に言うと、 DateInterval オブジェクトが保持している情報は、 ある date/time オブジェクトから別の date/time オブジェクトに情報を移す手順です。 この過程は必ずしも可逆ではありません。

DateInterval オブジェクトを生成する一般的なやり方は、 2つの date/time オブジェクトの差分を DateTimeInterface::diff() 経由で計算することです。

日付の間隔を比較する良い方法は定義されていません。 よって、DateInterval のインスタンスは 比較できません

クラス概要

class DateInterval {
/* プロパティ */
public int $y;
public int $m;
public int $d;
public int $h;
public int $i;
public int $s;
public float $f;
public int $invert;
public mixed $days;
/* メソッド */
public __construct(string $duration)
public static createFromDateString(string $datetime): DateInterval|false
public format(string $format): string
}

プロパティ

警告

以下に示すプロパティが使えるかどうかは、 PHP のバージョンに依存します。 そして、readonly と見なすべきです。

y

年。

m

月。

d

日。

h

時間。

i

分。

s

秒。

f

マイクロ秒。1秒の100万分の1です。

invert

間隔が負の数になっている場合は 1、そうでない場合は 0DateInterval::format() を参照ください。

days

DateTimeImmutable::diff()DateTime::diff() で作られた DateInterval オブジェクトの場合は、開始日と終了日の間の(丸一日、つまり0時から24時までの一日全体という意味での)日の数。 それ以外の場合は daysfalse となります。

from_string

DateInterval::createFromDateString() で作られた DateInterval オブジェクトの場合は、 このプロパティの値は true になり、 date_string の値が収集されます。 そうでない場合、この値は false になり、 y, f, invert, days の値が収集されます。

date_string

DateInterval::createFromDateString() の引数として使われる文字列。

変更履歴

バージョン 説明
8.2.0 DateInterval::createFromDateString() で作られた DateInterval のインスタンスのために、 プロパティ from_stringdate_string が追加されました。
8.2.0 プロパティ y から f, invert, days のみがアクセス可能になりました。
7.4.0 DateInterval インスタンスは、 比較できなくなりました。 これより前のバージョンでは、 全ての DateInterval のインスタンスが等しいとみなされていました。
7.1.0 f プロパティが追加されました

目次

add a note

User Contributed Notes 3 notes

up
34
cb
1 year ago
If you want to reverse a date interval use array_reverse and iterator_to_array. I've found using invert to be unreliable.

<?php
$start_date
= date_create("2021-01-01");
$end_date = date_create("2021-01-05"); // If you want to include this date, add 1 day

$interval = DateInterval::createFromDateString('1 day');
$daterange = new DatePeriod($start_date, $interval ,$end_date);

function
show_dates ($dr) {
foreach(
$dr as $date1){
echo
$date1->format('Y-m-d').'<br>';
}
}

show_dates ($daterange);

echo
'<br>';

// reverse the array

$daterange = array_reverse(iterator_to_array($daterange));

show_dates ($daterange);

?>

Gives
2021-01-01
2021-01-02
2021-01-03
2021-01-04

2021-01-04
2021-01-03
2021-01-02
2021-01-01
up
2
julio dot necronomicon at gmail dot com
2 months ago
More simple example i use to add or subtract.

<?php
$Datetime
= new Datetime('NOW', new DateTimeZone('America/Bahia'));
$Datetime->add(DateInterval::createFromDateString('2 day'));

echo
$Datetime->format("Y-m-d H:i:s");
?>
up
-1
nateb at gurutechnologies dot net
3 years ago
Many people have commented on doing a reverse interval on a date time. I personally find a backwards year to be a little strange to think about and instead opt to work with just intervals. This is the easiest I have found.

<?php
$one_year
= new DateInterval('P1Y');
$one_year_ago = new DateTime();
$one_year_ago->sub($one_year);
?>

Instead of:

<?php
$one_year_ago
= new DateInterval( "P1Y" );
$one_year_ago->invert = 1;
$one_year_ago = new DateTime();
$one_year_ago->add($one_year);
?>
To Top