【问题标题】:Get last three months from a list of month从月份列表中获取最近三个月
【发布时间】:2017-01-14 07:38:11
【问题描述】:
我必须从月份列表中找到最近三个月。
例如:-
月份列表是:-
Jun16,Mar16,Dec16,Jan17,Oct16,Nov16
所以我需要
Jan17
Dec16
and Nov16
最近三个月
有什么办法可以找到吗?
【问题讨论】:
标签:
php
codeigniter
date
time
【解决方案1】:
请尝试这种方式,它会 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);
?>
希望它会奏效。如果它完成了,请告诉我:)