本文介绍了找不到类'DateInterval'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取不包括最后日期的两个日期之间的日期,以下代码将用于查找日期。但是它触发了一个错误:
I want to get the date between two dates excluding last date, following code i will used to find dates. But it trigger an error saying:
代码:
$start = new DateTime('2014-08-06');
$end = new DateTime('2014-09-06');
$oneday = new DateInterval("P1D");
$days = array();
$data = "7.5";
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
$day_num = $day->format("N");
if($day_num < 6) {
$days[$day->format("Y-m-d")] = $data;
}
}
print_r($days);
推荐答案
看起来您的php版本没有 DateInterval
类。这是一个替代方案,使用 strtotime()
:
Looks like your version of php doesn't have the DateInterval
class. Here is an alternative using strtotime()
:
$start = '2014-08-06';
$end = '2014-09-06';
$num_days = floor((strtotime($end)-strtotime($start))/(60*60*24));
$data = '7.5';
$days = array();
for ($i=0; $i<$num_days; $i++)
if (date('N', strtotime($start . "+ $i days")) < 6)
$days[date('Y-m-d', strtotime($start . "+ $i days"))] = $data;
print_r($days);
结果:
Array
(
[2014-08-06] => 7.5
[2014-08-07] => 7.5
[2014-08-08] => 7.5
[2014-08-11] => 7.5
[2014-08-12] => 7.5
[2014-08-13] => 7.5
[2014-08-14] => 7.5
[2014-08-15] => 7.5
[2014-08-18] => 7.5
[2014-08-19] => 7.5
[2014-08-20] => 7.5
[2014-08-21] => 7.5
[2014-08-22] => 7.5
[2014-08-25] => 7.5
[2014-08-26] => 7.5
[2014-08-27] => 7.5
[2014-08-28] => 7.5
[2014-08-29] => 7.5
[2014-09-01] => 7.5
[2014-09-02] => 7.5
[2014-09-03] => 7.5
[2014-09-04] => 7.5
[2014-09-05] => 7.5
)
这篇关于找不到类'DateInterval'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!