从月份列表

时间:2017-01-14 07:38:12

标签: php codeigniter date time

我必须从月份清单中找到最后三个月。

例如: -

月份清单是: -

Jun16,Mar16,Dec16,Jan17,Oct16,Nov16

所以我需要

Jan17
Dec16
and Nov16

过去三个月

有没有办法找到?

1 个答案:

答案 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);

?>

希望它会奏效。如果它完成了,请告诉我。)