Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
除日期外,将PHP生成的数据周期列表拆分为月份和年份_Php_Sorting_Date_Datetime - Fatal编程技术网

除日期外,将PHP生成的数据周期列表拆分为月份和年份

除日期外,将PHP生成的数据周期列表拆分为月份和年份,php,sorting,date,datetime,Php,Sorting,Date,Datetime,因此,我使用下面的代码生成两个日期之间所有日期的无序列表。它工作得很好,但我想打印它的方式,它创建年和月嵌套列表,然后日期,即 - 2019 - February - 02-01-2019 - January - 01-01-2019 - 01-02-2019 ... ... - 2018 ... - 2017 ... 我可以做些什么来轻松实现这一点 <? $begin = new DateTime( '

因此,我使用下面的代码生成两个日期之间所有日期的无序列表。它工作得很好,但我想打印它的方式,它创建年和月嵌套列表,然后日期,即

 - 2019
   - February
     - 02-01-2019
   - January
     - 01-01-2019
     - 01-02-2019
     ...
   ...
 - 2018
   ...
 - 2017
   ...
我可以做些什么来轻松实现这一点

<?    
$begin = new DateTime( '2017-03-12' );
$end = new DateTime('2019-02-15');

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$sdcheck = array();

foreach($daterange as $date){
    $sdcheck[] = "<li>".$date->format("m-d-Y")."</li>";
}

?>

<ul>
<?
$sdcheck = array_reverse($sdcheck);
echo implode($sdcheck);
?>
</ul>


我做了一些修改,使之“更容易”。我没有反转不同级别的数组,而是对其进行了更改,以便反向生成日期周期。我还根据日期组织了临时数组,因此它现在是
[2019][4][][
,因此它已经分为年和月

然后是使用嵌套的
foreach
循环生成不同级别的列表的情况

$begin = new DateTime( '2018-03-16' );
$end = new DateTime();

$interval = DateInterval::createFromDateString('-1 day');
$daterange = new DatePeriod($end, $interval ,$end->diff($begin)->days);
$sdcheck = array();
foreach($daterange as $date){
    $sdcheck[$date->format("Y")][$date->format("F")][] = "<li>".$date->format("m-d-Y")."</li>";
}

$output = "<ul>";
foreach ( $sdcheck as $year=>$months )   {
    $output .="<li>".$year."<ul>";
    foreach ( $months as $month=>$dates )   {
        $output .="<li>".$month."<ul>";
        $output .=implode($dates)."</ul>".PHP_EOL;
    }
    $output .="</ul>";
}
$output .="</ul>";

echo $output;
$begin=新日期时间('2018-03-16');
$end=新的日期时间();
$interval=DateInterval::createFromDateString('-1天');
$daterange=新的日期周期($end,$interval,$end->diff($begin)->天);
$sdcheck=array();
foreach($daterange作为$date){
$sdcheck[$date->format(“Y”)][$date->format(“F”)][]=“
  • ”。$date->format(“m-d-Y”)。“
  • ”; } $output=“
      ”; foreach($sdcheck作为$year=>$months){ $output.=“
    • ”“$year。”
        ”; foreach($月份为$月份=>$日期){ $output.=“
      • ”“$month。”
          ”; $output.=内爆($dates)。“
        ”。PHP\u EOL; } $output.=“
      ”; } $output.=“
    ”; echo$输出;
    一个简单的方法是:

    
    function createList($daterange)
    {
        $sdcheck = [];
        foreach($daterange as $date){   
            $sdcheck[$date->format('Y')][$date->format('F')][$date->format("m-d-Y")] = $date->format("m-d-Y");
        }
    
        $html = '';
        foreach ($sdcheck as $key => $element) {
            $html .= '<ul><li>' . $key;
            if (is_array($element)) {
                $html .= createList($element);
            }
            $html .= '</li></ul>';
        }
        return $html;
    }
    
    
    函数createList($daterange)
    {
    $sdcheck=[];
    foreach($daterange作为$date){
    $sdcheck[$date->format('Y')][$date->format('F')][$date->format(“m-d-Y”)]=$date->format(“m-d-Y”);
    }
    $html='';
    foreach($sdcheck作为$key=>$element){
    $html.='
    • '.$key; if(is_数组($element)){ $html.=createList($element); } $html.='
    '; } 返回$html; }
    我同意另一个答案,但你的答案也不错!