[英]How to detect if first day of the week is Saturday, Sunday, or Monday?
What is a dependancy free solution for my PHP application to detect the first day of the week for the running php process?什么是我的 PHP 应用程序检测正在运行的 php 进程的一周中的第一天的无依赖解决方案? Example:
例子:
I have tried this:我试过这个:
$first_day_of_week = date('l', strtotime('This week'));
I expected the code to return Sunday for american users and Monday for european.我希望代码在星期日返回给美国用户,星期一返回给欧洲用户。 But it always returns Monday (an ISO-8601 week calendar).
但它总是返回星期一(ISO-8601 周历)。
Athough not a dependancy free solution.虽然不是一个无依赖的解决方案。 Here is detecting week type using the PHP extension Intl.
这是使用 PHP 扩展 Intl 检测周类型。
if (extension_loaded('intl')) {
switch (IntlCalendar::createInstance()->getFirstDayOfWeek()) {
case 1: $first_day_of_week = 'Sunday'; break; // Western
case 2: $first_day_of_week = 'Monday'; break; // ISO-8601
case 7: $first_day_of_week = 'Saturday'; break; // Middle Eastern
}
} else {
trigger_error('PHP is missing the Intl extension', E_USER_WARNING);
}
The first day of the week returned by一周的第一天返回
$no = IntlCalendar::createInstance()->getFirstDayOfWeek();
depends on your php.ini.取决于你的 php.ini。 You get the default value with locale_get_default().
您可以使用 locale_get_default() 获得默认值。 IntlCalendar::createInstance() accepts a locale as the 2nd parameter, which then delivers different results for the first day of the week.
IntlCalendar::createInstance() 接受语言环境作为第二个参数,然后为一周的第一天提供不同的结果。
foreach(['en_AE','en_US','en_GB'] as $locale){
$no = IntlCalendar::createInstance(null,$locale)->getFirstDayOfWeek();
$weekDayName = date('l', strtotime('Saturday +'.$no.' days'));
echo $locale.' : '.$weekDayName.'<br>';
}
Output:输出:
en_AE : Saturday
en_US : Sunday
en_GB : Monday
Regardless of this, the following applies for date and DateTime:无论如何,以下适用于日期和日期时间:
Start of week is always ISO-8601 in PHP date functions.
PHP 日期函数中的每周开始始终是 ISO-8601。
(as already noted in the comment) (如评论中所述)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.