Get Start and End Days for a Given Week in PHP

前端 未结 16 1891
囚心锁ツ
囚心锁ツ 2020-12-01 03:49

I\'m trying to get the week range using Sunday as the start date, and a reference date, say $date, but I just can\'t seem to figure it out.

For example,

16条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 04:14

    Without doing so much string manipulation, you can do some simple math on the timestamps.

    function getDateRange(&$start, &$end, $date) {
      $seconds_in_day = 86400;
      $day_of_week = date("w", $date); 
      $start = $date - ($day_of_week * $seconds_in_day);
      $end = $date + ((6 - $day_of_week) * $seconds_in_day);
    }
    

提交回复
热议问题