duanbi1888 2015-04-17 08:59
浏览 31
已采纳

寻找接下来的13个星期一和上周一

I have the following code that return's the next 13 Mondays from today's date.

for($i=1; $i<=13; $i++){
    echo date("Y-m-d", strtotime('+'.$i.' Monday'))."<br>";
}

I want to be able to amend this so it not only shows the next 13 Mondays but the Monday that has just past.

I tried amending the code as follows but I then get two instances of the next Monday returned.

for($i=-1; $i<=13; $i++){
echo date("Y-m-d", strtotime('+'.$i.' Monday'))."<br>";
}

Data returned.

2015-04-13
2015-04-20  //<--
2015-04-20
2015-04-27
2015-05-04
2015-05-11
2015-05-18
2015-05-25
2015-06-01
2015-06-08
2015-06-15
2015-06-22
2015-06-29
2015-07-06
2015-07-13

Any ideas on how I achieve this?

  • 写回答

6条回答 默认 最新

  • dqc18251 2015-04-17 09:06
    关注

    I would do it like this:

    for($i =- 1; $i <= 13; $i == 0 ? $i += 2 : $i++){
        echo date("Y-m-d", strtotime("$i Monday")) . "<br>";
    }
    

    Using a ternary operator to check if $i is 0 - and if, increase it by 2 instead of 1 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭