dongzhong8834 2015-07-17 11:44
浏览 37
已采纳

PHP按年获取周数和日期[关闭]

I was wondering how i can get all week numbers and dates where the week start and ends on. I am completely empty on the code, so I cannot show you anything. I need to get something like this website: http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php

  • 写回答

2条回答 默认 最新

  • double820122 2015-07-17 12:15
    关注

    I would suggest the following, just change the year to any year you like.

    <?php
    
    $year = 2014;
    
    $weeks = getIsoWeeksInYear($year);
    
    for($x=1; $x<=$weeks; $x++){
        $dates = getStartAndEndDate($x, $year);
        echo $x . " - " . $dates['week_start'] . ' - ' . $dates['week_end'] . "<br>";
    }
    
    function getIsoWeeksInYear($year) {
        $date = new DateTime;
        $date->setISODate($year, 53);
        return ($date->format("W") === "53" ? 53 : 52);
    }
    
    function getStartAndEndDate($week, $year) {
      $dto = new DateTime();
      $ret['week_start'] = $dto->setISODate($year, $week)->format('Y-m-d');
      $ret['week_end'] = $dto->modify('+6 days')->format('Y-m-d');
      return $ret;
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改