Get Start and End Days for a Given Week in PHP

前端 未结 16 1872
囚心锁ツ
囚心锁ツ 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:16

    Use this to get the "week" number from any given date.

    //October 29, 2009 is my birthday
    echo $week date('W', strtotime('2009-10-29'));
    //OUTPUT: 44
    //October 29 is the 44th week in the year 2009
    

    Now pass the parameters for getWeekDates function as "year (2009)" and "$week".

    function getWeekDates($year, $week, $start=true){
            $from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
            $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week
    
            if($start) {
                return $from;
            } else {
                return $to;
            }
            //return "Week {$week} in {$year} is from {$from} to {$to}.";
        }
    

    For More Info Please refer this link http://blog.ekini.net/2009/07/09/php-get-start-and-end-dates-of-a-week-from-datew/

提交回复
热议问题