How can I get the week number by considering Sunday as the start day?

$date = 'some date'; $todays_week = date('W',strtotime($date)); 

This code gives me the week number, considered on Monday as the first day of the week. I want to get the week number, considering Sunday as the starting day of the week. How to do it?

+4
source share
2 answers

As I know, there is no way to change the starting day. So, you can check if the current day is Sunday, and if so, increase the week number with 1:

 function getIsoWeeksInYear($year) { $date = new DateTime; $date->setISODate($year, 53); return ($date->format("W") === "53" ? 53 : 52); } function getWeek($date) { $week = date('W',strtotime($date)); $day = date('N',strtotime($date)); $max_weeks = getIsoWeeksInYear(date('Y',strtotime($date))); if($day == 7 && $week != $max_weeks) { return ++$week; } elseif($day == 7) { return 1; } else { return $week; } } echo getWeek('2012-12-30'); 
0
source

The most elegant solution is to simply add 1 day to enter:

 $todays_week = date('W', strtotime($date) + 60 * 60 * 24 ); 

The only time this does not work is where December 31st is also Sunday. In this case, the answer will incorrectly say 1. You will need to check this.

0
source

All Articles