Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Incorrect first day of the week for my timezone

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

like image 640
Mathieu Dumoulin Avatar asked Jun 22 '16 10:06

Mathieu Dumoulin


1 Answers

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.

like image 146
impeto Avatar answered Oct 07 '22 23:10

impeto