1

昇順でソートされた日付配列があります。日付を次のように表示したい

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 日の日付を取得しました。ありがとう、

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 に答える