2017-01-14 8 views
0

私は月のリストから最後の3か月を見つけなければなりません。例えば月のリストから最後の3か月を取得する

: - 月の

一覧は以下のとおりです。 -

Jun16、Mar16、Dec16、Jan17、Oct16、Nov16

ので、私は必要

Jan17 
Dec16 
and Nov16 

最終3ヶ月

ありがとうございますyを見つけるには?

+0

あなたはすでに試したことを見せていただけますか? –

+0

月名文字列の長さ3は固定されますか? –

+0

と月リストは文字列形式か、配列形式になりますか? –

答えて

0

この方法を試してみてください、それは100%に動作します

<?php 

$month_arr = array('Jun16','Mar16','Dec16','Jan17','Oct16','Nov16'); 

/* Convert dates in y-m-d format */ 
foreach ($month_arr as $key => $ma) { 
    $output = str_split($ma, 3); 
    $full_date = $output[1].'-'.date('m', strtotime($output[0])).'-01'; 
    $months[] = $full_date; 
} 

/* Now sort all dates in DESC order */ 
$dates = array(); 
foreach ($months as $u) 
{ 
    $dates[] = $u; 
} 
array_multisort($dates, SORT_DESC, $months); 

/* Get latest 3 months */ 
$last_three_months = array_slice($months, 0, 3); 

/* Convert last 3 months into required format */ 
foreach($last_three_months as $lm) 
{ 
    $parsed_last_three_months[] = date('My',strtotime($lm)); 
} 

echo "<pre>"; 
print_r($parsed_last_three_months); 

?> 

は、それが動作します願っています。完了したかどうかお知らせください。

+0

ありがとうございました。それは – Diksha

+0

あなたの歓迎を働く... :) –

+0

良い質問だった:) –

関連する問題