首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

DatePeriod::__construct

(PHP 5 >= 5.3.0, PHP 7)

DatePeriod :: __ construct - 创建一个新的DatePeriod对象

描述

public DatePeriod::__construct ( DateTimeInterface $start , DateInterval $interval , int $recurrences , int $options )

public DatePeriod::__construct ( DateTimeInterface $start , DateInterval $interval , DateTimeInterface $end , int $options )

public DatePeriod::__construct ( string $isostr , int $options )

创建一个新的DatePeriod对象。

参数

start

期间的开始日期。

interval

期间内重复发生的时间间隔。

recurrences

重复次数。

end

期间的结束日期。

isostr

ISO 8601重复间隔规范。

options

可以设置为DatePeriod::EXCLUDE_START_DATE从该时段内的重复日期组中排除开始日期。

更新日志

描述

5.5.8

结束类型更改为DateTimeInterface。以前,DateTime。

5.5.0

开始类型更改为DateTimeInterface。以前,DateTime。

例子

示例 #1 DatePeriod example

代码语言:javascript
复制
<?php
$start?=?new?DateTime('2012-07-01');
$interval?=?new?DateInterval('P7D');
$end?=?new?DateTime('2012-07-31');
$recurrences?=?4;
$iso?=?'R4/2012-07-01T00:00:00Z/P7D';

//?All?of?these?periods?are?equivalent.
$period?=?new?DatePeriod($start,?$interval,?$recurrences);
$period?=?new?DatePeriod($start,?$interval,?$end);
$period?=?new?DatePeriod($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";
}
?>

上面的例子将输出:

代码语言:javascript
复制
2012-07-01
2012-07-08
2012-07-15
2012-07-22
2012-07-29

示例#2 DatePeriod示例 DatePeriod::EXCLUDE_START_DATE

代码语言:javascript
复制
<?php
$start?=?new?DateTime('2012-07-01');
$interval?=?new?DateInterval('P7D');
$end?=?new?DateTime('2012-07-31');

$period?=?new?DatePeriod($start,?$interval,?$end,
?????????????????????????DatePeriod::EXCLUDE_START_DATE);

//?By?iterating?over?the?DatePeriod?object,?all?of?the
//?recurring?dates?within?that?period?are?printed.
//?Note?that,?in?this?case,?2012-07-01?is?not?printed.
foreach?($period?as?$date)?{
????echo?$date->format('Y-m-d')."\n";
}
?>

上面的例子将输出:

代码语言:javascript
复制
2012-07-08
2012-07-15
2012-07-22
2012-07-29

注意

由ISO指定的重复的未结合的号码8601的第4.5节“周期性时间间隔”不支持,即,既不通过“R / ...”作为isostr也不传递NULL作为end将工作。

← DatePeriod

DatePeriod::getDateInterval →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com