2014-01-29 76 views
-2

請幫助分組日期(dd/mm/yyyy)。 我有數據這樣 01/01/2014,02/01/2014,03/01/2014,04/02/2014,05/02/2014,07/03/2014使用PHP對日期進行分組

我想組或想要的結果是這樣

1,2,3 January 2014, 04,05 Febrary 2014, 07 March 2014.

+2

你有沒有試圖通過你自己做到這一點?如果是,那麼也在這裏添加你的代碼。我們會幫你解決這個問題。 – vinaykrsharma

回答

-1
$raw_date = '01/01/2014,02/01/2014,03/01/2014,04/02/2014,05/02/2014,07/03/2014'; 
# Using DD/MM/YYYY formats strtotime understands as MM/DD/YYYY 
# So replace all '/' to '-'. So strtotime understand formats as DD-MM-YYYY 
$raw_date = str_replace('/', '-', $raw_date); 
$separated = explode(',', $raw_date); 
$collection = array(); 
foreach($separated as $e) { 
    $e = date('j|F Y', strtotime($e)); 
    $e = explode('|', $e); 
    if(!isset($collection[$e[1]])) { 
     $collection[$e[1]] = ''; 
    } 
    $collection[$e[1]] .= $e[0].","; 
} 

foreach ($collection as $my => $d) { 
    echo rtrim($d, ',') . " $my\n"; 
} 
0

好極了! @ vinay-kr-sharma,但我添加了一些更多的代碼把我的月語言

function groupdate($dateis){ 
    //add my date language [ID/Indonesia] 
    $monthID[1]="Januari"; 
    $monthID[2]="Pebruari"; 
    $monthID[3]="Maret"; 
    $monthID[4]="April"; 
    $monthID[5]="Mei"; 
    $monthID[6]="Juni"; 
    $monthID[7]="Juli"; 
    $monthID[8]="Agustus"; 
    $monthID[9]="September"; 
    $monthID[10]="Oktober"; 
    $monthID[11]="Nopember"; 
    $monthID[12]="Desember"; 
    $raw_date = $dateis; 
    $raw_date = str_replace('/', '-', $raw_date); 
    $separated = explode(',', $raw_date); 
    $collection = array(); 
    foreach($separated as $e) { 
     //Add Some code to put the my language month 
     $e = date('j', strtotime($e))."|".$monthID[date('n', strtotime($e))]." ".date('Y', strtotime($e)); 
     $e = explode('|', $e); 
     if(!isset($collection[$e[1]])) { 
      $collection[$e[1]] = ''; 
     } 
     $collection[$e[1]] .= $e[0].","; 
    } 
    $result=""; 
    foreach ($collection as $my => $d) { 
     $result=$result.rtrim($d, ',') . " $my, "; 
    } 
    return rtrim($result, ', '); 
    }; 
相關問題