doubiao9775 2015-12-17 08:32
浏览 49
已采纳

PHP日期:2015/2016的周数

As stated in ISO 8601 there are years that will have 53 weeks, 2015 is one of those. What will be the next week number in 2016? Does it start with week 01 again? The reason I am asking is that in one of my projects an URL "2016-W01" does show the wrong days (eg. Mon. 2nd Jan 2016 - where it should be Mon. 4th Jan 2016). If I am calling "2016-W02" the correct days are coming up (4th-10th Jan.), but "obviously?" this should be week01 and not week2..

Any ideas on this? Thanks!

  • 写回答

2条回答 默认 最新

  • doushi8186 2015-12-17 08:49
    关注
    <?php
    $dt = new DateTime('2015-12-27');
    for($i=0; $i<12; $i++) {
        echo $dt->format('Y-m-d , D, W'), "
    ";
        $dt->modify('+ 1 day');
    }
    

    prints (without the descriptions)

    2015-12-27 , Sun, 52
    2015-12-28 , Mon, 53\
    2015-12-29 , Tue, 53|
    2015-12-30 , Wed, 53|
    2015-12-31 , Thu, 53 <-- this thursday is in 2015, therefore this is week 53
    2016-01-01 , Fri, 53|
    2016-01-02 , Sat, 53|
    2016-01-03 , Sun, 53/
    
    2016-01-04 , Mon, 01\
    2016-01-05 , Tue, 01|
    2016-01-06 , Wed, 01|
    2016-01-07 , Thu, 01 <-- this is the first thursday in 2016, therefore this is week 1
    

    edit and btw: You might want to switch your project to use the standard php date/time parser:

    <?php
    $dt = new DateTime('2016W01'); echo $dt->format('Y-m-d, D, W'), "
    ";
    $dt = new DateTime('2016W02'); echo $dt->format('Y-m-d, D, W'), "
    ";
    

    prints

    2016-01-04, Mon, 01
    2016-01-11, Mon, 02
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题