How do I get the first day of the week for the current locale? (PHP / L8N)
From CLDR's supplemental data (release 36, October 2019):
<firstDay day="mon" territories="001 AD AI AL AM AN AR AT AX AZ BA BE BG BM BN BY CH CL CM CR CY CZ DE DK EC EE ES FI FJ FO FR GB GE GF GP GR HR HU IE IS IT KG KZ LB LI LK LT LU LV MC MD ME MK MN MQ MY NL NO NZ PL RE RO RS RU SE SI SK SM TJ TM TR UA UY UZ VA VN XK"/>
<firstDay day="fri" territories="MV"/>
<firstDay day="sat" territories="AE AF BH DJ DZ EG IQ IR JO KW LY OM QA SD SY"/>
<firstDay day="sun" territories="AG AS AU BD BR BS BT BW BZ CA CN CO DM DO ET GT GU HK HN ID IL IN JM JP KE KH KR LA MH MM MO MT MX MZ NI NP PA PE PH PK PR PT PY SA SG SV TH TT TW UM US VE VI WS YE ZA ZW"/>
<firstDay day="sun" territories="GB" alt="variant" references="Shorter Oxford Dictionary (5th edition, 2002)"/>
(territory 001
is the UN M49 area code for "World")
how do I find out which is the first day of the week in the current locale?
I guess you'll have to write that function yourself. From this answer:
From CLDR's supplemental data:
<firstDay day="mon" territories="001 AD AI AL AM AN AT AX AZ BA BE BG BM BN BY CH CL CM CR CY CZ DE DK EC EE ES FI FJ FO FR GB GE GF GP GR HR HU IS IT KG KZ LB LI LK LT LU LV MC MD ME MK MN MQ MY NL NO PL PT RE RO RS RU SE SI SK SM TJ TM TR UA UY UZ VA VN XK" />
<firstDay day="fri" territories="BD MV" />
<firstDay day="sat" territories="AE AF BH DJ DZ EG IQ IR JO KW LY MA OM QA SD SY" />
<firstDay day="sun" territories="AG AR AS AU BR BS BT BW BZ CA CN CO DM DO ET GT GU HK HN ID IE IL IN JM JP KE KH KR LA MH MM MO MT MX MZ NI NP NZ PA PE PH PK PR PY SA SG SV TH TN TT TW UM US VE VI WS YE ZA ZW" />
<firstDay day="sun" territories="GB" alt="variant" references="Shorter Oxford Dictionary (5th edition, 2002)" />
(Territory 001 is "World")
PHP DateTime() class, change first day of the week to Monday
I found this to work, yet there are some inconsistencies in PHP's DateTime class.
If the departing date is a sunday the previous monday is not considered the same week (fixed by this class). But departing from a monday, the next sunday is considered as the same week. If they fix that in the future this class will need some additions.
class EuroDateTime extends DateTime {
// Override "modify()"
public function modify($string) {
// Change the modifier string if needed
if ( $this->format('N') == 7 ) { // It's Sunday and we're calculating a day using relative weeks
$matches = array();
$pattern = '/this week|next week|previous week|last week/i';
if ( preg_match( $pattern, $string, $matches )) {
$string = str_replace($matches[0], '-7 days '.$matches[0], $string);
}
}
return parent::modify($string);
}
}
How to determine the first day of week in python
I could only figure out how to do this with the Babel library. It's available through easy_install
.
>>> import babel
>>> locale = babel.Locale('en', 'US')
>>> locale.first_week_day
6
>>> locale.days['format']['wide'][locale.first_week_day]
u'Sunday'
turns out that the following doesn't work as Nate was kind enough to point out. If anybody knows why, please post and answer showing how to do it right. This should be doable with the standard library.
If you just want the number of the day, then you can use calendar.LocalTextCalendar
.
>>> import calendar
>>> c = calendar.LocaleTextCalendar(locale='de_DE') # locale=('en_US', 'UTF8') fails too.
>>> c.firstweekday
0
There is also the iterweekdays
method.
>>> list(c.iterweekdays())
[0, 1, 2, 3, 4, 5, 6]
Python get Start of Week According to Timezone/Locale
Not that I am aware of.
This information is not in pytz database. It would be available in locale database, but your system probably doesn't have all possible locales installed (on linux systems locale -a shows what is in there). You could in theory install them all and build a converter between a timezone and a locale, and check it from there.
Far too complicated to my taste.
I would just do this manually. There are only so many countries in the world, and most of them use Monday as the first day of week. Just build a dictionary with about 200 entries, you can import the data for example from here: http://chartsbin.com/view/41671
Hannu
First day of week from HTML5 Intl API
The official vanilla API for retrieving this is:
new Intl.Locale('en-US').weekInfo
which will return an object like:
{
firstDay: 7, // First day of the week is Sunday
minimalDays: 1, // First calendar week of the year must have at least 1 weekday
weekend: [6, 7] // Weekend is Saturday and Sunday
}
Examples for other cultures: (new Intl.Locale('<locale>').weekInfo
)
Germany de-DE
:
{
firstDay: 1, // First day of the week is Monday
minimalDays: 4, // First calendar week of the year has at least 4 weekdays
weekend: [6, 7] // Weekend is Saturday and Sunday
}
Egypt ar-EG
:
{
firstDay: 6, // First day of the week is Saturday
minimalDays: 1, // First calendar week of the year has at least 1 weekday
weekend: [5, 6] // Weekend is Friday and Saturday
}
Uganda sw-UG
:
{
firstDay: 1, // First day of the week is Monday
minimalDays: 1, // First calendar week of the year has at least 1 weekday
weekend: [7] // Weekend is Sunday only
}
Brunei ms-BN
:
{
firstDay: 7, // First day of the week is Sunday
minimalDays: 1, // First calendar week of the year has at least 1 weekday
weekend: [5, 7] // Weekend is Friday and Sunday
}
AngularJS i18n: ngLocale FIRSTDAYOFWEEK and WEEKENDRANGE
For ngLocale
files, "Monday is day 0 as specified by ISO-8601"
...as opposed to Javascript, where Date.prototype.getDay()
returns 0 for Sunday. Watch out!
I asked for this to be added to the documentation.
Related Topics
Strip_Tags() Function Blacklist Rather Than Whitelist
PHP $String{0} VS. $String[0];
Laravel 5 - Session Doesn't Work
How to Read the Query String in PHP and HTML
Adding User Custom Field Value to Order Items Details
Check If Method Exists in the Same Class
Why Ob_Start() Must Come Ahead of Session_Start() to Work in PHP
PHP Post Limited to 1000 Variables
Wherejsoncontains Laravel 5.6 Not Working
Select Statement with Fetch_Array in MySQLi Prepared Statements
From the String Name of a Class, How to Get a Static Variable
How to Detect If a User Uploaded a File Larger Than Post_Max_Size
Change "Billing Details" Text to "Shipping Details" on Woocommerce Checkout Page
Converting Dates with PHP for Datetime in SQL