简体   繁体   中英

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).

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". The example shows "2" for "Tuesday"

When asking the IntlDateCalendar, my system shows day "2" (which is monday) as the first day of the week. So the IntlDateFormatter should give today, monday, as first day of the week.

Using "EEEE" or "eeee" as input makes no difference. 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> .

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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