我需要一个显示活动和非活动月份的php
函数。
我的阵列:
$dates = array('2014-08','2014-09','2014-11','2014-12','2015-02',)
预期输出:
2014-08 Active
2014-09 Active
2014-10 Inactive
2014-11 Active
2014-12 Active
2015-01 Inactive
2015-02 Active
这应该适合您:
<?php
$dates = array('2014-08', '2014-09', '2014-11', '2014-12', '2015-02');
$begin = new DateTime($dates[0]);
$end = new DateTime(end($dates) . "+1 month");
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date)
echo $date->format("Y-m") . (in_array($date->format("Y-m") , $dates) ? " active" : " inactive") . "<br />";
?>
输出:
2014-08 active
2014-09 active
2014-10 inactive
2014-11 active
2014-12 active
2015-01 inactive
2015-02 active
我发现这个解决方案有效
参考 https://gist.github.com/anonymous/c1dc49c23ddc0813a156/42dc128b9cfaf04675c5fd8c6d267b6c507f34cf
试试这段代码: 参考
<?php
$dates = array('2014-08', '2014-09', '2014-11', '2014-12', '2015-02');
$begin = new DateTime( '2014-08' );
$end = new DateTime( '2015-02' );
$end = $end->modify( '+1month' );
$interval = new DateInterval('P1M');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
if(in_array($date->format("Y-m") , $dates)){
echo $date->format("Y-m") . "Active"."<br>";
}else{
echo $date->format("Y-m") . "Inactive"."<br>";
}
}?>