【问题标题】:Is there a more efficient way of creating a list of years/months?有没有更有效的方法来创建年/月列表?
【发布时间】:2010-09-25 03:50:47
【问题描述】:

在一个页面上,我想动态列出年份和每年的所有月份,以便可以查看每个月的存档。我想先显示当前年份,但当前年份可能还没有结束,所以我只想显示已经过去的月份和当前月份。然后我想要自今年以来(即 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');
    }
} 

【问题讨论】:

  • 为什么要存储这些值?如果您在不先存储它们的情况下简单地显示这些值,您可能会更快地执行此操作。
  • 需要将它们存储起来,以便将它们传递给用于模板的 Smarty。

标签: php language-agnostic datetime performance


【解决方案1】:
$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;
    }
}

这似乎有点简单...是不是更快...?

【讨论】:

  • 是的,它似乎更快。 0.000255 0.000293 0.000291 = 平均 0.000279
【解决方案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>';
    }
}

【讨论】:

  • 这将输出过去 5 年的所有月份,而对于今年,我只想输出过去的月份,包括当前月份。 2008 年 12 月很好,因为所有月份都过去了,但在 2009 年 2 月,我只想显示一月和二月。
  • 您的意思是在 2009 年 2 月它会显示 2004 年 3 月..12 月?由于 -1 个月是从今天开始工作的,因此它永远不会显示未来的日期,因为 strtotime 中有一个负值。
  • 我已经添加了if,但是家里没有php服务器来测试它
  • 谢谢,但我不想要过去 5 年,我只想要从 2008 年开始。基本测试你的代码显示它比我的慢:0.009551 0.010196 0.011607 = avrg 0.0105
【解决方案3】:

修改

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

【讨论】:

  • 谢谢,但您的代码速度较慢。我的代码在 0.000391 0.000319 和 0.000316 中执行,平均为 0.000342。您的代码在 0.000361 0.000326 和 0.000325 中执行,平均为 0.000349。差异很小,但您的代码看起来也更复杂,因此可读性较差。
  • 我的代码更灵活。我还发现阅读 2 页多余的代码更容易。
  • 我需要的代码不需要很灵活。你会发现你的文章更容易阅读,因为它是你写的。
【解决方案4】:
$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;
}

【讨论】:

    【解决方案5】:

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

      -> Get current Year
    
      -> Loop the current year untill the current month is reached
    
         -> Print Month
    

    【讨论】:

    • 谢谢,但我知道该怎么做。我想知道我的代码是否可以提高效率。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2016-06-19
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多