如何提供过去 6 个月内应动态更改的 6 个按钮

我需要一一对应的 6 个按钮,例如这些按钮是过去 6 个月到当前月份的月份名称


November

December

January

February

March

April

所以如果月份确实发生变化,按钮应该动态变化,就像


December

January

February

March

April

May

金迪帮助我。


注意:为此我们可以使用 javascript 或 jquery 或 php。


三国纷争
浏览 94回答 3
3回答

茅侃侃

这是 PHP 中的:$result=array();for($months=5;$months>=0;$months--){&nbsp; &nbsp; if(!$months){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$month_name = date("F");&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$month_name = date("F", strtotime("-$months month"));&nbsp; &nbsp; }&nbsp; &nbsp; $result[]='<button>'.$month_name.'</button>';}echo implode('<br>', $result);

UYOU

你可以在 php 中这样做:<?phpecho "<button> " . date('F') . "</button>";&nbsp;for ($i = 1; $i < 6; $i++) {&nbsp; echo "<button> " . date('F', strtotime("-$i month")) . "</button>";}?>

胡说叔叔

我用 JavaScript 解决了const monthNames = ["January", "February", "March", "April", "May", "June",&nbsp; "July", "August", "September", "October", "November", "December"];const maxDateCnt = 11;const countFiveMonts = [];const dateTime = new Date();let monthCnt = dateTime.getMonth();let monthCpy = monthCnt;for(let i = 0;&nbsp; i < 6; i++){&nbsp; &nbsp;let current;&nbsp; &nbsp; if(monthCnt > maxDateCnt){&nbsp; &nbsp; &nbsp; &nbsp; monthCnt -= 12;&nbsp; &nbsp; &nbsp; &nbsp; monthCpy = monthCnt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;current = monthNames[monthCnt];&nbsp; &nbsp;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; current = monthNames[monthCpy + i];&nbsp; &nbsp; }&nbsp; &nbsp;countFiveMonts.push(current);&nbsp; &nbsp;monthCnt++;}console.log(countFiveMonts);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5