dongyuqi3808 2014-03-10 16:11
浏览 28
已采纳

PHP将年份和月份添加到选择框中

I'm writing codes for show years and months of past years

ex

<select>                                        
    <option value="03/2014">03/2014</option>                                        
    <option value="02/2014">02/2014</option>                                        
    <option value="01/2014">01/2014</option>                                        
    <option value="12/2013">12/2013</option>                                        
    <option value="11/2013">11/2013</option>                                        
    <option value="10/2013">10/2013</option>
 </select>

im using this code but i can get only moonth names

 $i = 1;
$month = time();
while($i <= 12)
{
    $month_name = date('F', $month);
    echo '<option value="'. $month_name. '">'.$month_name.'</option>';
    $month = strtotime('+1 month', $month);
    $i++;
}

but i need to get month and year for past 4,5 years, anyone know how to that using php date function or any other short way.thank you

  • 写回答

2条回答 默认 最新

  • duanfaxin7014 2014-03-10 16:26
    关注

    Put something like this into a function:

    $start = strtotime('2014-01');
    $end = strtotime('2014-12');
    $range = array(date('Y-m', $start) => date('Y-m', $start));
    while ( $start <= strtotime('-1 month', $end) ) {
        $start = strtotime('+1 month', $start);
        $yearMonth = date('Y-m', $start);
        $range[$yearMonth] = $yearMonth;
    }
    var_dump($range);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line