7

I need to get week number in php where week should be calculated from sunday. By default its from monday. Please help me to find a way how to get week number considering sunday as starting day.

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

But I need to get week number of year, weeks starting on sunday.

thanks

Hriju
  • 728
  • 1
  • 16
  • 27
  • This link may help you : http://stackoverflow.com/questions/5376484/issue-with-getting-week-starting-from-sunday –  Apr 17 '13 at 09:57
  • 9
    Get the week number starting from Monday. Then, if the date in question is a Sunday, decrement the week number. Be careful to not go lower than 0. – Jon Apr 17 '13 at 10:00
  • 1
    You wanted to say "Increment" – Dmitry Aug 28 '13 at 19:37

10 Answers10

5

Try this:

$week = intval(date('W'));

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

echo $week;

Not sure if the logic is right though;

silkfire
  • 24,585
  • 15
  • 82
  • 105
4

The first solution is not correct on Jan 01, 2017 or any year that begins on a Sunday.

Try this:

$date = date('Y-m-d');
echo strftime("%U", strtotime($date ) );
Case
  • 4,244
  • 5
  • 35
  • 53
Eman Jayme
  • 230
  • 2
  • 8
3

To expand on silkfire answer and allow for it wrapping around 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++;
        }
    }
Community
  • 1
  • 1
hounded
  • 666
  • 10
  • 21
2

Try this one. to get sunday day must -1 day.

$date = "2015-05-25";
echo date("W", strtotime("-1 day",strtotime($date)));
Alghi Fari
  • 440
  • 3
  • 12
1

You should try with 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('d-M-Y'); //and just prints with formatting 
Foobar
  • 196
  • 1
  • 8
1

I solved this 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;
}
ppiatek
  • 69
  • 1
  • 5
0

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

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

if $date("w") returns true means today is a day between tuesday and sunday (1-6), so, we can return today week ('today').

if returns false, it means is monday (0), so, we should return the next day ('+1 week').

This way we don't need to care about last or first day of year or check if current year has 52 or 53 weeks.

Edited: the previous answer (and others in this topic) doesn't work for this year because januray 1st is monday, so, it needs to be 1 week ago (-1 week) excluding sunday (day 6).

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

I think a condition asking if januray 1st is monday could work, but I didn't test it yet, I will come back with an answer later

For a custom day you could 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);
}
stramin
  • 2,183
  • 3
  • 29
  • 58
0

Building on @silkfire's 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')); 
Laith
  • 409
  • 6
  • 13
0

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

I needed to ADD day instead of subtracting to get Alghi Fari's answer to work.

$date = "2022-11-13";
echo date("W", strtotime("+1 day",strtotime($date)));
ACKmiecik
  • 70
  • 8