[英]php generate a select box having dropdown years of -5 years and +5 years
[英]PHP get years and month to a select box
我正在寫顯示年份和過去幾年的代碼
前
<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>
即時通訊使用此代碼,但我只能得到月名
$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++;
}
但我需要獲得過去4,5年的月份和年份,任何人都知道如何使用php date函數或任何其他簡短方法。謝謝
將以下內容放入函數中:
$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);
dd a
$year_name=date('Y',$month);
...以及$month_name
並相應地調整select
的option
。
或者,你可以做
$month_name = date('F/Y', $month);
另外,4.5年是4 * 12 + 6 = 54個月,所以while($i <= 54)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.