1

is there any parameter or configuratión to provide to a DateTime object to not consider week number in iso but taking the first day of january as the first week of the year?

As Datetime works with an ISO rule, the first week of the year is the week with the first thursday.

I need to make some operations with datetimes but i need it to consider the first day of January as the first week of the year, even if its a Sunday.

Is this possible? I tried everything.

Thank you.

Alwork
  • 103
  • 10

4 Answers4

2

Don’t think that is possible with the native DateTime, no - that only follows ISO 8601 rules. You’d have to handle that yourself, by taking the difference between Jan 1st vs Jan 4th as start of the first week into account in all places where you operate with those values.

You should consider using a date library such as Carbon - with the proper locale set, that should be able to do what you want.

https://carbon.nesbot.com/docs/#api-week:

Week methods follow the rules of the current locale (for example with en_US, the default locale, the first day of the week is Sunday, and the first week of the year is the one that contains January 1st)

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Nice thank you. Actually im using Carbon, so this is usefull for me. I need something like you say, but i need to keep monday as the first day of the week. Is there any locale for that configuration? – Alwork Jun 11 '20 at 09:43
  • Don’t know, you’d have to go find a list of available locales somewhere, and check for yourself. Or you modify the settings of an existing one - https://carbon.nesbot.com/docs/#api-localization mentions `Carbon::setWeekStartsAt`, but says it’s deprecated - but is also says, _“You can also use the 'first_day_of_week' locale setting to change the start of week according to current locale selected”_ – CBroe Jun 11 '20 at 09:58
0

There is no option to set the start weekday of first week. You can only use following format for week number of the day:

  • %U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
  • %V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)
  • %W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week

So if you want to calculate week number assuming 1st day of year as first week of year, then you have to make your own function to add 1 if 1st Jan is not Thursday or before.

Emre Kayaoglu
  • 561
  • 2
  • 5
  • 19
0

Taken from my own answer and adjusted for dateTime and the year starting with week 1.

function get_week($date){
    $w=(int)$date->format('W');
    $m=(int)$date->format('n');
    return $w==1?($m==12?53:1):($w>=51?($m==1?1:$w):$w);
    }

2016-01-01 is officially week 53, but the function returns 1. Week 1 now has 10 days, till 2016-01-01. Week 2 starts at 2016-01-11

Michel
  • 4,076
  • 4
  • 34
  • 52
0

Function below provides week number 01-53 starting 1st of January. Note: in such way the first and the last week of the year could be less than 7 days.

function getWeekNumberStartingJan1(DateTime $date)
{
    if ((clone $date)->modify('1 january')->format('W') !== '01') {
        if ($date->format('o') !== $date->format('Y')) {
            return '01';
        }
        
        return sprintf('%02d', (int)$date->format('W') + 1);
    }

    return $date->format('W'); //year started from Monday
}
Vladislav Ross
  • 541
  • 5
  • 6