简体   繁体   English

每周第一天PHP IntlDateFormatter

[英]First day of week PHP IntlDateFormatter

I'm trying to get the "correct" day of the week using the IntlDateFormatter class (uses ICU in background). 我正在尝试使用IntlDateFormatter类(在后台使用ICU)获得“正确的”星期几。

The code explains the problem 该代码说明了问题

<?php
$timezone = new \DateTimeZone('Europe/Amsterdam');
$date = new \DateTime('2018-01-15', $timezone);
$formatter = new \IntlDateFormatter('nl_NL', null, null);

// This prints "Monday" which is corrent
echo $formatter->formatObject($date, 'EEEE');

// This prints "2", which is incorrect
echo $formatter->formatObject($date, 'e');

According to the ICU documentation this should print "1". 根据ICU文档,应打印“ 1”。 The example shows "2" for "Tuesday" 该示例显示“ 2”代表“星期二”

When asking the IntlDateCalendar, my system shows day "2" (which is monday) as the first day of the week. 当询问IntlDateCalendar时,我的系统将第2天(星期一)显示为一周的第一天。 So the IntlDateFormatter should give today, monday, as first day of the week. 因此,IntlDateFormatter应该在今天(星期一)作为一周的第一天。

Using "EEEE" or "eeee" as input makes no difference. 使用“ EEEE”或“ eeee”作为输入没有区别。 Other timezones/locales do not change the outcome. 其他时区/地区不会改变结果。

The following code worked for me, although I started the week from Sunday, you can play around with it: 以下代码对我有用,尽管我从星期日开始一周,但是您可以尝试一下:

$day = date('w'); $week_start = date('d F Y', strtotime('-'.$day.' days'));

Then I put it in a header like this: <h4>Schedule for the week beginning SUN <?=$week_start?></h4> . 然后,将其放在这样的标题中: <h4>Schedule for the week beginning SUN <?=$week_start?></h4>

Result: Click link to see a picture of the result. 结果: 单击链接以查看结果图片。

Also, check out PHP docs: http://php.net/manual/en/function.strtotime.php , http://php.net/manual/en/function.date.php or this: https://www.w3schools.com/php/func_date_date.asp 另外,请查看PHP文档: http : //php.net/manual/en/function.strtotime.php,http: //php.net/manual/en/function.date.php或以下网址https:// www .w3schools.com / php / func_date_date.asp

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM