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
}
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.
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.
(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.
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.
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 />';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With