Php week number starting sunday

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

ini_set('date.timezone''Europe/Lisbon');
ini_set('intl.default_locale''es_ES');$cal IntlCalendar::createInstance();
$cal->set(2013/* June */30); // A Sundayvar_dump($cal->getFirstDayOfWeek()); // 2 (Monday)echo IntlDateFormatter::formatObject($cal, <<'local day of week: 'cc'
week of month    : 'W'
week of year     : 'ww
EOD
), 
"\n";$cal->setFirstDayOfWeek(IntlCalendar::DOW_SUNDAY);

echo

IntlDateFormatter::formatObject($cal, <<'local day of week: 'cc'
week of month    : 'W'
week of year     : 'ww
EOD
), 
"\n";

int(2)
local day of week: 7
week of month    : 4
week of year     : 26
local day of week: 1
week of month    : 5
week of year     : 27

Php week number starting sunday

I'm adding a simple post here with a PHP method that has helped me. This method calculates the beginning and ending of a week given the year and week number. The problem I've run into is that “first day of the week” is subjective. Some people believe the first day of the week is “Monday” while others believe the first day of the week is “Sunday”. ISO-8601 specifies the first day of the week as “Monday”. Whereas, most western calendars display Sunday as the first day of the week and Saturday as the last day of the week.

To add to the confusion, PHP's methods themselves seem confused about what the first and last day of the week are.

For example:

$new_date = new DateTime;
// returns Monday, Jan 29 2018
$new_date->setISODate(2018, 5);

// returns Sunday, Feb 4 2018
$new_date->modify('sunday this week');

// returns Sunday, Jan 28 2018
$new_date->setISODate(2018, 5 ,0);

You'll notice that the string "sunday this week" actually returns Sunday, Feb 4 whereas setting the date to the 0 day of the same week returns Sunday, Jan 28. I'm not saying that Sunday doesn’t happen twice a week… but Sunday doesn’t happen twice a week.

All this to say, the method below is the one I've found returns the most helpful results:

function get_first_and_last_day_of_week( $year_number, $week_number ) {
    // we need to specify 'today' otherwise datetime constructor uses 'now' which includes current time
    $today = new DateTime( 'today' );

    return (object) [
        'first_day' => clone $today->setISODate( $year_number, $week_number, 0 ),
        'last_day'  => clone $today->setISODate( $year_number, $week_number, 6 )
    ];
}

Cross Posting from https://jeremysawesome.com/2019/08/12/php-get-first-and-last-day-of-week-by-week-number/

How do I start a week from Sunday in PHP?

$firstday = date ( 'l - d/m/Y' , strtotime ( "sunday 1 week" ));

Does the week start on Sunday or Monday?

Monday is the first day of the week, according to the international standard for the representation of dates and times ISO 8601. However, in the United States and Canada, Sunday is considered to be the start of the week. This is because of religious reasons.

How to get week start date in PHP?

php $weekstart = strtotime('monday this week'); $weekstop = strtotime('sunday this week 23:59:59'); //echo date('d.m.Y H:i:s', $weekstart) . ' - '. date('d.m.Y H:i:s', $weekstop); ?>

What week is week 1?

The ISO 8601 definition for week 01 is the week with the first Thursday of the Gregorian year (i.e. of January) in it.