Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first and last week days according to locale

I'm trying to find first and last week days based on the locale.

In U.S.A. a week normally starts on Sunday, but in other countries it could start on another day - e.g. Monday or even Saturday.

setlocale(LC_ALL, "en_US.UTF-8");
date_default_timezone_set("America/New_York");

$start_week = (new DateTimeImmutable());
$start_week = $start_week->modify('this week');
$end_week = $start_week->modify('this week +6 days');

$interval = new DateInterval('P1D');
$week_range = new DatePeriod($start_week, $interval, $end_week);

foreach($week_range as $week_day) {
  // $week_day starts with Monday, supposed to be Sunday
}
like image 222
Alex G Avatar asked Nov 30 '16 16:11

Alex G


People also ask

How to set the day of the week according to locale?

Gets or sets the day of the week according to the locale. This dependent on Weekday plugin to work If the locale assigns Sunday as the first day of the week, dayjs ().weekday (0) will be Sunday. If Monday is the first day of the week, dayjs ().weekday (0) will be Monday.

How can I get the first day of the week from localdate?

The LocalDate doesn't seem to have this, but WeekFields (which is from the java-8 API) does ( here ). So you can do this: This returns the value of the first day of week, starting from 1 as Monday, ending with 7 as Sunday.

How to return the beginning of the week based on date?

(1) This formula =A2-MOD (A2-2,7) will return Monday as the beginning of week based on the given date. For returning Sunday as the start of a week, please apply this formula =A2-MOD (A2-1,7). (2) If returned Monday or Sunday doesn’t show as date, please keep selecting the dates and click the Home > Number Format drop down list > Short Date.

How to display the start and end of the week date span?

This article helps in displaying the start of the week date span and end of the week date span, which will be displayed, using SQL query. It returns a particular date with the particular number interval added to a particular date part of the date. DATEPART () function returns the integer value of particular datepart of the passed date.


1 Answers

One way to do this is to use the IntlCalendar class. It has a method getFirstDayofWeek(), which returns an integer, corresponding to the DOW_ constants in IntlCalendar:

const integer DOW_SUNDAY = 1 ;
const integer DOW_MONDAY = 2 ;
const integer DOW_TUESDAY = 3 ;
const integer DOW_WEDNESDAY = 4 ;
const integer DOW_THURSDAY = 5 ;
const integer DOW_FRIDAY = 6 ;
const integer DOW_SATURDAY = 7 ;

Use that to add days to the start day when calling DateTimeImmutable::modify() for the start day. See it in action with three locales (i.e. en_US, es_ES, sw_KE) in this phpfiddle.

$locale = 'es_ES'; //Spain Spanish locale
$cal1 = IntlCalendar::createInstance(NULL, $locale);
$firstDayOfWeek = $cal1->getFirstDayOfWeek();
$daysToAdd = $firstDayOfWeek - 2; //difference from US M-Sunday 
echo 'locale: '.$local.' first day of week: '.$cal1->getFirstDayOfWeek().' days to add: '.$daysToAdd.'<br />';
$start_week = new DateTimeImmutable();
$start_week = $start_week->modify('this week +'.$daysToAdd.' days');
$end_week = $start_week->modify('+6 days');
$interval = new DateInterval('P1D');
$week_range = new DatePeriod($start_week, $interval, $end_week);

foreach($week_range as $week_day) {
    echo 'week day: '.$week_day->format('l m/d/Y').'<br />';
}
like image 196
Sᴀᴍ Onᴇᴌᴀ Avatar answered Oct 20 '22 01:10

Sᴀᴍ Onᴇᴌᴀ