循环创建之前12个月的列表

编程入门 行业动态 更新时间:2024-10-28 06:30:21
本文介绍了循环创建之前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个月的列表

本文发布于:2023-10-21 17:03:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个月   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!