PHP Incorrect first day of the week for my timezone
Asked Answered
T

2

8

strange problem here.

I'm using Laravel but i'm pretty sure it has nothing to do with it per se, and my Carbon dates are always returning "monday" as the first day of the week. Problem is, i'm in a locale where it should be returning "Sunday".

/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'America/Montreal',

Create a Carbon date and print it:

<?php
$date = Carbon::now();
var_dump($date);

Outputs

object(Carbon\Carbon)[278]
    public 'date' => string '2016-06-22 06:05:18.000000' (length=26)
    public 'timezone_type' => int 3
    public 'timezone' => string 'America/Montreal' (length=16)

And if i print the first day of the week

<?php var_dump($date->getWeekStartsAt());

I get

1

Strangely enough, if i go to my homestead console and type "locale", i get:

LANG=en_US.UTF-8
LANGUAGE=en_US:
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

So my default locale should be USA right? According to Google, first day of the week in the USA, Canada and Japan is Sunday... Running "locale first_weekday" yields: 1 (Monday)

So i'm not sure what i should or can do to fix this as this is completely incorrect. I have a calendar being drawn based on machine locale and this is obviously wrong so it is showing my customers a calendar that doesn't fit their locale.

Thanks for sharing your thoughts!


EDIT #1

Here is the link to the Carbon issue: https://github.com/briannesbitt/Carbon/issues/680

Tybi answered 22/6, 2016 at 10:27 Comment(1)
By looking in github.com/briannesbitt/Carbon/blob/master/src/Carbon/…, I can't really see where setWeekStartsAt is used. Maybe Carbon does not do it automatically ? Plus, in their tests, they don't really test it with a real timezone, but manually : github.com/briannesbitt/Carbon/blob/master/tests/Carbon/…Overdye
L
5

Try this: in AppServiceProvider::boot() method set the start and end of week like this:

Carbon\Carbon::setWeekStartsAt(Carbon\Carbon::SUNDAY);
Carbon\Carbon::setWeekEndsAt(Carbon\Carbon::SATURDAY);

You must set both the start and end of the week. Setting just the start of the week to Sunday will make the week only a day long.

Lugubrious answered 20/10, 2017 at 18:31 Comment(1)
setWeekStartsAt() and setWeekEndsAt() are causing deprecated warning.Demurrage
G
4

The method getWeekStartsAt() returns a static property

protected static $weekStartsAt = self::MONDAY;

which is set to self::MONDAY which resolves to 1. That's why you get the 1.

You can call setWeekStartsAt($day) to set the correct start day of the week. Of course you then need to call setWeekEndsAt($day).

$date->setWeekStartsAt(0);
$date->setWeekEndsAt(6);
// Or better
$date->setWeekStartsAt(Carbon::SUNDAY);
$date->setWeekEndsAt(Carbon::SATURDAY);
Gawen answered 22/6, 2016 at 13:11 Comment(2)
I opened a bug at Carbon, this is most probably not the intended behavior and truly makes no sense! Who would want to set that manually on each run of their app. It should come from the locale which is then the next real problem, why am i getting monday when my locale is set to US or CA.Tybi
@MathieuDumoulin I can understand your reaction. Could you share the link of the bug report? I would also be interested what the creator saysGawen

© 2022 - 2024 — McMap. All rights reserved.