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

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

问题描述

有没有一种方法可以使用PHP循环基于当前月份(不包括当前月份)创建前12个月的以下列表?

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

该值应始终是月份的第一天(格式:yyyy-mm-dd),下拉菜单本身应仅显示年份和月份(格式:yyyy-mm):

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>
<? } ?>

推荐答案

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

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

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