1

我有一个按 asc 顺序排序的日期数组。我想将日期显示为

Oct 10,12,24 2012
Dec 12,20,24 2012
Jan 02,10,25 2013

我有日期作为月份,即 10 月、12 月、1 月 .. 以及日期和年份,但我希望它以上述格式显示。我已经尝试了下面的代码,但它没有给出预期的结果。有人可以帮我吗?$CruiseDetailsS​​ailing 是包含升序日期的数组。

  if (count($CruiseDetailsSailing) > 0) {
                            $date = array();
                            $month = array();
                            $Year = array();
                            for ($n = 0; $n < count($CruiseDetailsSailing); $n++) {
                                if ($CruiseDetailsSailing[$n] != "") {
                                    $date[] = date('Y-m-d', strtotime($CruiseDetailsSailing[$n]));
                                }
                            }
                        }
                        sort($date);
                        if (count($date) > 0) {
                            $temp = "";
                            $yeartemp = "";
                            for ($p = 0; $p < count($date); $p++) {
                                $month = date("M", strtotime($date[$p]));
                                $day = date("d", strtotime($date[$p]));
                                $year = date("Y", strtotime($date[$p]));
                                if ($month != $temp) {
                                    $temp = $month;
                                    ?>
                                    <li> <?php
                        echo $temp . " " . $day . ", ";
                    } else {
                        echo $day . ", ";
                    }
                    if (($p != 0) && ((date("M", strtotime($date[$p]))) == (date("M", strtotime($date[$p - 1])))) && ((date("Y", strtotime($date[$p]))) == (date("Y", strtotime($date[$p - 1]))))) {
                        echo $year;
                    }
                    if (($p != 0) && ((date("M", strtotime($date[$p]))) != (date("M", strtotime($date[$p - 1])))) && ((date("Y", strtotime($date[$p]))) != (date("Y", strtotime($date[$p - 1]))))) {
                        echo $year;
                    }
                }

Oct 是月份,10,12,24 是月份,2012 是年份。我得到的日期为 2012 年 10 月 10 日、2012 年 10 月 12 日、2012 年 10 月 24 日。我只想将结果显示为 10 月一次,然后将日期和年份显示一次。谢谢,

4

2 回答 2

1

这应该这样做:

sort($date); // sorted input array containing dates

$result_dates = Array();

// bring dates in correct format
foreach($date as $current_date) {
  $month = date("M", strtotime($current_date));
  $day = date("d", strtotime($current_date));
  $year = date("Y", strtotime($current_date));
  $result_dates[$year][$month][] = $day;
}

// output dates
foreach($result_dates as $year => $months) {
  foreach($months as $month => $days) {
    echo $month . " " . implode(",", $days) . " " . $year . "\n";
  }
}

以下输入:

$date = Array('10/03/2012', '10/10/2012', '10/17/2012', '11/04/2012', '11/05/2012', '11/23/2012');

结果是:

Oct 03,10,17 2012
Nov 04,05,23 2012
于 2012-10-11T06:10:53.117 回答
0

你可以像这样简单地做到这一点

/* Array to pass the function   
$date_array =   array(
                '2012-10-10',
                '2012-12-10',
                '2012-24-10'
                );
*/

function getMyDateFormat($date_array){

    $days   =   array();
    $year   =   '';
    $month  =   '';

    sort($date_array);  

    if(count($date_array)>0){


        foreach($date_array as $row)
        {
            $days[] =   date('d',strtotime($row));
            $year   =   date('Y',strtotime($row));
            $month  =   date('M',strtotime($row));
        }

        $new_date   =   $month  .' '.   implode(',',$days)  .' '.   $year;
        return  $new_date;
    }
}   
于 2012-10-11T06:20:55.937 回答