PHP:循环创建前12个月的列表 [英] PHP: loop to create list of previous 12 months

查看:251
本文介绍了PHP:循环创建前12个月的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据当前月份(不包括当前月份),有没有办法使用PHP循环创建一个像以前的12个月一样的列表?
该值应该始终是本月的第一个(格式:yyyy-mm-dd),下拉列表本身应该只显示年和月(格式:yyyy-mm):

 < option value =2014-03-01> 2014-03< / option> 
< option value =2014-02-01> 2014-02< / option>
< option value =2014-01-01> 2014-01< / option>
< option value =2013-12-01> 2013-12< / option>
< option value =2013-11-01> 2013-11< / option>
< option value =2013-10-01> 2013-10< / option>
// ...

我尝试过以下,但似乎有一些错误,这不工作:

 <?php for($ i = 0; $ i< = 12; $ i ++){ ?> 
< option value =<?php echo date('Ym-d',strtotime( - 1个月));?"><?php echo date ',strtotime( - 1个月)); ?>< / option>
< }?>

非常感谢任何帮助,蒂姆。

解决方案

 <?php 
for($ i = 0; $ i'= 12; $ i ++){
echo'< option value ='。date('Ym-d',strtotime( - $ i month))'>'。date 'Y-m',strtotime( - $ i month))'< / option>';
}


is there a way I can use a PHP loop to create a list like the following with the previous 12 months, based on the current month (excluding the current month) ? The value should always be the first of the month (format: yyyy-mm-dd) and the dropdown itself should just show year and month (format: yyyy-mm):

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

I tried the following but seem to have something wrong there as this is not working:

<?php for ($i=0; $i<=12; $i++) { ?>
    <option value="<?php echo date('Y-m-d', strtotime("-1 month")); ?>"><?php echo date('Y-m', strtotime("-1 month")); ?></option>
<? } ?>

Many thanks for any help with this, Tim.

解决方案

<?php
for ($i=0; $i<=12; $i++) { 
echo '<option value="'.date('Y-m-d', strtotime("-$i month")).'">'.date('Y-m', strtotime("-$i month")).'</option>';
 } 

这篇关于PHP:循环创建前12个月的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆