是否有更有效的方法来创建年/月的列表?

时间:2008-12-03 16:14:37

标签: php language-agnostic datetime performance

在页面上,我想动态列出每年的年份和所有月份,以便查看每个月的存档。我想先显示当前年份,但今年可能还没有结束,所以我只想显示已经过去的月份和当月。那么我想要从今年开始的所有年份和所有月份(即2008年)。

我创建的PHP代码完成了以下工作。有没有更有效的方法来实现这一目标?我正在运行PHP 5.2。

$current_year = date('Y');
$months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

// Loop through all the months and create an array up to and including the current month
foreach ($months as $month)
{
    if ($month <= date('m'))
    {
        switch ($month)
        {
            case 1:
                $years[$current_year][] = 'January';
                break;
            case 2:
                $years[$current_year][] = 'February';
                break;
            case 3:
                $years[$current_year][] = 'March';
                break;
            case 4:
                $years[$current_year][] = 'April';
                break;
            case 5:
                $years[$current_year][] = 'May';
                break;
            case 6:
                $years[$current_year][] = 'June';
                break;
            case 7:
                $years[$current_year][] = 'July';
                break;
            case 8:
                $years[$current_year][] = 'August';
                break;
            case 9:
                $years[$current_year][] = 'September';
                break;
            case 10:
                $years[$current_year][] = 'October';
                break;
            case 11:
                $years[$current_year][] = 'November';
                break;
            case 12:
                $years[$current_year][] = 'December';
                break;
        }
    }
}

// Previous years
$years_to_create = $current_year - 2008;

if (!empty($years_to_create))
{
    for ($i = 1; $i <= $years_to_create; $i++)
    {
        $years[$current_year - $i] = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    }
} 

5 个答案:

答案 0 :(得分:2)

尝试这样做,它将在过去5年中列出一个月/年的下降列表

$years = 5;

for ($i = 0; $i < (12* $years); $i++)
{
    if (date('Y',strtotime("-".$i." month")) > (date('Y') - $years) )
    {
      echo date('F Y',strtotime("-".$i." month")) . '<br />'; 
    }
}

然后,如果你需要在数组中,只需像你一样添加到

$years = 5;
$myarray = array();

for ($i = 0; $i < (12* $years); $i++)
{
    if (date('Y',strtotime("-".$i." month")) > (date('Y') - $years) )
    {
         $year_key = date('Y') - date('Y',strtotime("-".$i." month")); 
         $myarray[$year_key][] = date('F',strtotime("-".$i." month"));
    }
}

//Write it out to the screen
foreach ($myarray as $yearkey=>$eachyear)
{
    foreach ($eachyear as $eachmonth)
    {   
        echo (date('Y')-$yearkey) . ' ' . $eachmonth . '<br>';
    }
}

答案 1 :(得分:2)

$current_year = date('Y');
$months = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$month_names = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Loop through all the months and create an array up to and including the current month

for ($month=1;$month<=date('m');$month++)
{
$years[$current_year][] = $month_names[$month-1];
}

// Previous years
$years_to_create = $current_year - 2008;

if (!empty($years_to_create))
{
    for ($i = 1; $i <= $years_to_create; $i++)
    {
        $years[$current_year - $i] = $month_names;
    }
}

这看起来有点简单......是否更快......?

答案 2 :(得分:1)

function getMonthsFromYear($start = 2008, $end = null, array $months = null) {
    static $sMonths = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    $thisYear = intval(date('Y'));
    if (!is_array($months) || (count($months) < 12))
        $months = $sMonths;
    if (!is_int($end))
         $end = $thisYear;
    if ($end > $thisYear)
        return array_fill($start, $end - $start + 1, $months);
    if ($start < $end)
        $monthsInDuration = array_fill($start, $end - $start, $months);
    $monthsInDuration[$end] = array_slice($months, 0, (int) date('m'));
    return $monthsInDuration;
}
print_r(getMonthsFromYear());
print_r(getMonthsFromYear(2006));
print_r(getMonthsFromYear(2008, 2010));

答案 3 :(得分:1)

$current_year = date('Y');
$month_names = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$years[$current_year] = array_slice($month_names, 0, date('m'));

for ($i = 2008; $i < $current_year; $i++) {
   $years[$i] = $month_names;
}

答案 4 :(得分:0)

PHP有一个Date()函数和一个mktime函数,你可以使用它们来比较日期和创建字符串等日期。如果没有提供代码,我可以给你一个指令以某种方式进行迭代:

  -> Get current Year

  -> Loop the current year untill the current month is reached

     -> Print Month