How to get weeks starting on sunday?

浪子不回头ぞ 提交于 2019-11-27 06:56:02

问题


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


回答1:


Try this:

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

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

echo $week;

Not sure if the logic is right though;




回答2:


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 ) );



回答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++;
        }
    }



回答4:


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

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



回答5:


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 



回答6:


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);
}



回答7:


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')); 



回答8:


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;
}



回答9:


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;
}


来源:https://stackoverflow.com/questions/16057039/how-to-get-weeks-starting-on-sunday

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!