How to start the week on Sunday?

I need to get the week number in php where the week should be calculated from Sunday. By default it is from Monday. Please help me find a way to get the week number with Sunday as the starting day.

In php manual ISO-8601, week is the year number, weeks starting on Monday (added in PHP 4.1.0)

But I need to get the weekly number of the year, the weeks starting on Sunday.

thanks

+6
source share
9 answers

Try the following:

$week = intval(date('W')); if (date('w') == 0) { // 0 = Sunday $week++; } echo $week; 

Not sure if the logic is correct

+5
source

The first decision is incorrect on January 1, 2017, or in any year that begins on Sunday.

Try it:

 $date = date('Ym-d'); echo strftime("%U", strtotime($date ) ); 
+4
source

To expand on silkfire's answer and allow it to carry over around the years

  if($date->format('w') == 0){ if(date('W',strtotime($date->format('Y')."-12-31"))==52 and $date->format('W') == 52){ $week = 1; } elseif(date('W',strtotime($date->format('Y')."-12-31"))==53 and $date->format('W') == 53){ $week = 1; } else{ $week++; } } 
+3
source

Try it. to get a Sunday, you need -1 day.

 $date = "2015-05-25"; echo date("W", strtotime("-1 day",strtotime($date))); 
+2
source

You should try strftime

 $week_start = new DateTime(); $week = strftime("%U"); //this gets you the week number starting Sunday $week_start->setISODate(2012,$week,0); //return the first day of the week with offset 0 echo $week_start -> format('dM-Y'); //and just prints with formatting 
+1
source

I know this topic is old, but this is a shorter way to do this using the elvis operator and the +7 day expression for strtotime ():

 $week=date("W",strtotime(date("w")==1?"+7 day":"+0 day")); 

if $ date ("w") returns true, then today is the day between Tuesday and Sunday (1-6), so we can return today for a week ("today").

if it returns false, it means Monday (0), so we must return the next day ("+1 week").

Thus, we do not need to care about the last or first day of the year or check whether the current year has 52 or 53 weeks.

Edited: the previous answer (and others in this section) does not work this year because januray 1st is Monday, so it should be 1 week ago (-1 week), except Sunday (6th day).

 date("W",strtotime(date("w")?'-7 day':'+0 day')); 

I think a condition asking if januray 1st monday can work, but I have not tested it yet, I will come back with an answer later

For a custom day, you can use this:

 $date = strtotime('2018-04-30'); // (it is monday) if(date("w",strtotime(date('Y',$date).'-01-01'))==1){ // if first day of year is monday $week = strtotime(date('w',$date)?"-7 day":"+0 day",$date); // and today is sunday, sub a week $week = date("W",$week); }else{ // if is not monday $week = strtotime(date('w',$date)==1?"+7 day":"+0 day",$date); // and today is monday, add a week $week = date("W",$week); } 
0
source

Based on @silkfire answer:

  $year = date('Y'); $week_no = date('W'); if (date('w') == 0) { // 0 = Sunday $week_no++; } // We shifted the week but the week still starts on a Monday. $weekStartDate = new DateTime(); $weekStartDate->setISODate($year,$week_no); // Shift start date to Sunday $weekStartDate->add(DateInterval::createFromDateString('-1 day')); 
0
source

Tested in php 5.6 Debian 7

 function getWeekNumber(\DateTime $_date) { $week = intval($_date->format('W')); if(intval($_date->format('w')) == 0) { $week = intval($_date->format('W')) == ( 52 + intval($_date->format('L')) ) ? 1 : $week + 1; } return $week; } 
0
source

I solved it like this:

 function getWeekOfYear( DateTime $date ) { $dayOfweek = intval( $date->format('w') ); if( $dayOfweek == 0 ) { $date->add(new DateInterval('P1D')); } $weekOfYear = intval( $date->format('W') ); return $weekOfYear; } 
0
source

All Articles