【问题标题】: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

最近三个月

有什么办法可以找到吗?

【问题讨论】:

  • 你能展示你已经尝试过的东西吗?
  • 月份名称字符串长度3将被固定?
  • 和月份列表将是字符串格式还是数组格式?
  • 是的,月份名称长度 3 是固定的
  • 是数组格式

标签: 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);

?>

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

【讨论】:

  • 是个好问题 :)
猜你喜欢
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 2021-01-03
  • 1970-01-01
相关资源
最近更新 更多