如何显示给定日期数组的活动和非活动月份
how to show show active and inactive month from given date array
我需要一个 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
试试这个代码:REFERENCE
<?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>";
}
}?>
我发现这个解决方案有效
参考https://gist.github.com/anonymous/c1dc49c23ddc0813a156/42dc128b9cfaf04675c5fd8c6d267b6c507f34cf
我需要一个 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
试试这个代码:REFERENCE
<?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>";
}
}?>
我发现这个解决方案有效
参考https://gist.github.com/anonymous/c1dc49c23ddc0813a156/42dc128b9cfaf04675c5fd8c6d267b6c507f34cf