欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

HP实现计算一年多少个星期,返回一个星期的开始时间和结束时间(可选返回时间戳或日期)

程序员文章站 2022-05-02 20:21:57
...
  1. function getWeekStartAndEnd ($year,$week=1) {
  2. header("Content-type:text/html;charset=utf-8");
  3. date_default_timezone_set("Asia/Shanghai");
  4. $year = (int)$year;
  5. $week = (int)$week;
  6. //按给定的年份计算本年周总数
  7. $date = new DateTime;
  8. $date->setISODate($year, 53);
  9. $weeks = max($date->format("W"),52);
  10. //如果给定的周数大于周总数或小于等于0
  11. if($week>$weeks || $week return false;
  12. }
  13. //如果周数小于10
  14. if($week $week = '0'.$week;
  15. }
  16. //当周起止时间戳
  17. $timestamp['start'] = strtotime($year.'W'.$week);
  18. $timestamp['end'] = strtotime('+1 week -1 day',$timestamp['start']);
  19. //当周起止日期
  20. $timeymd['start'] = date("Y-m-d",$timestamp['start']);
  21. $timeymd['end'] = date("Y-m-d",$timestamp['end']);
  22. //返回起始时间戳
  23. return $timestamp;
  24. //返回日期形式
  25. //return $timeymd;
  26. }
复制代码

可选, 一个星期, 多少个