PHP DateInterval 一月到六月和七月到十二月
PHP DateInterval Jan - June and July - Dec
正在尝试创建过去 2 年的列表或日期间隔,分为 6 个月的范围,即 2015 年 1 月 - 6 月、2015 年 7 月 - 12 月、2016 年 1 月 - 6 月......直到今天。
我查看了 DateTime 和 DateInterval,类似这样
$begin = new DateTime( '2015-01-01' );
$end = new DateTime( '2017-12-26' );
$interval = new DateInterval('P6M');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
echo sprintf("%s\n", $dt->format("Y-m-d"));
这给了我
2015-01-01
2015-07-01
2016-01-01
2016-07-01
2017-01-01
2017-07-01
无法确定每个时间段的开始和结束时间(1 月 1 日至 6 月 30 日和 7 月 1 日至 12 月 31 日)。
添加间隔后,减去一天。使用sub()
和new DateInterval('P1D')
表示减去一天
foreach ( $period as $dt )
echo sprintf(
"%s %s\n",
$dt->format("Y-m-d"),
$dt->add($interval)
->sub(new DateInterval('P1D'))
->format("Y-m-d"));
这输出:
2015-01-01 2015-06-30
2015-07-01 2015-12-31
2016-01-01 2016-06-30
2016-07-01 2016-12-31
2017-01-01 2017-06-30
2017-07-01 2017-12-31
更新: 在评论中,您还询问了如何指示日期代表的半年。答案太明显了:
sprintf(
"%s %s %d\n",
$dt->format("Y-m-d"),
$dt->add($interval)
->sub(new DateInterval('P1D'))
->format("Y-m-d"),
$dt->format("n")/6
);
注意%d
和相应的$dt->format("n")/6
。
我的输出:
2015-01-01 2015-06-30 1
2015-07-01 2015-12-31 2
2016-01-01 2016-06-30 1
2016-07-01 2016-12-31 2
2017-01-01 2017-06-30 1
2017-07-01 2017-12-31 2
您可以像这样为每个开始添加间隔大小“-1”:
$begin = new DateTime( '2015-01-01' );
$end = new DateTime( '2017-12-26' );
$interval = new DateInterval('P6M');
$period = new DatePeriod($begin, $interval, $end);
$oneDay = new DateInterval("P1D");
$oneDay->invert=1; #just get the previous day as if("P-1D")
foreach ( $period as $dt ){
echo $dt->format("Y-m-d"); #interval start
$dt->add($interval); #adding the interval size
$dt->add($oneDay); #get the last day of the cur interval
echo "<br>";
echo $dt->format("Y-m-d"); #interval end
echo "<br>";
}
正在尝试创建过去 2 年的列表或日期间隔,分为 6 个月的范围,即 2015 年 1 月 - 6 月、2015 年 7 月 - 12 月、2016 年 1 月 - 6 月......直到今天。
我查看了 DateTime 和 DateInterval,类似这样
$begin = new DateTime( '2015-01-01' );
$end = new DateTime( '2017-12-26' );
$interval = new DateInterval('P6M');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
echo sprintf("%s\n", $dt->format("Y-m-d"));
这给了我
2015-01-01
2015-07-01
2016-01-01
2016-07-01
2017-01-01
2017-07-01
无法确定每个时间段的开始和结束时间(1 月 1 日至 6 月 30 日和 7 月 1 日至 12 月 31 日)。
添加间隔后,减去一天。使用sub()
和new DateInterval('P1D')
表示减去一天
foreach ( $period as $dt )
echo sprintf(
"%s %s\n",
$dt->format("Y-m-d"),
$dt->add($interval)
->sub(new DateInterval('P1D'))
->format("Y-m-d"));
这输出:
2015-01-01 2015-06-30
2015-07-01 2015-12-31
2016-01-01 2016-06-30
2016-07-01 2016-12-31
2017-01-01 2017-06-30
2017-07-01 2017-12-31
更新: 在评论中,您还询问了如何指示日期代表的半年。答案太明显了:
sprintf(
"%s %s %d\n",
$dt->format("Y-m-d"),
$dt->add($interval)
->sub(new DateInterval('P1D'))
->format("Y-m-d"),
$dt->format("n")/6
);
注意%d
和相应的$dt->format("n")/6
。
我的输出:
2015-01-01 2015-06-30 1
2015-07-01 2015-12-31 2
2016-01-01 2016-06-30 1
2016-07-01 2016-12-31 2
2017-01-01 2017-06-30 1
2017-07-01 2017-12-31 2
您可以像这样为每个开始添加间隔大小“-1”:
$begin = new DateTime( '2015-01-01' );
$end = new DateTime( '2017-12-26' );
$interval = new DateInterval('P6M');
$period = new DatePeriod($begin, $interval, $end);
$oneDay = new DateInterval("P1D");
$oneDay->invert=1; #just get the previous day as if("P-1D")
foreach ( $period as $dt ){
echo $dt->format("Y-m-d"); #interval start
$dt->add($interval); #adding the interval size
$dt->add($oneDay); #get the last day of the cur interval
echo "<br>";
echo $dt->format("Y-m-d"); #interval end
echo "<br>";
}