简体   繁体   English

PHP我的时区一周的第一天不正确

[英]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. 我正在使用Laravel,但我很确定它本身与它无关,我的Carbon日期总是在“星期一”作为一周的第一天返回。 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: 奇怪的是,如果我去家园控制台并键入“locale”,我会得到:

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? 所以我的默认语言环境应该是USA吗? According to Google, first day of the week in the USA, Canada and Japan is Sunday... Running "locale first_weekday" yields: 1 (Monday) 根据Google的说法,本周美国,加拿大和日本的第一天是星期天...运行“locale first_weekday”收益率:1(星期一)

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 编辑#1

Here is the link to the Carbon issue: https://github.com/briannesbitt/Carbon/issues/680 以下是碳问题的链接: https//github.com/briannesbitt/Carbon/issues/680

The method getWeekStartsAt() returns a static property getWeekStartsAt()方法返回一个静态属性

protected static $weekStartsAt = self::MONDAY;

which is set to self::MONDAY which resolves to 1 . 设置为self::MONDAY ,解析为1 That's why you get the 1 . 这就是你获得1的原因。

You can call setWeekStartsAt($day) to set the correct start day of the week. 您可以调用setWeekStartsAt($day)来设置一周中正确的开始日期。 Of course you then need to call setWeekEndsAt($day) . 当然,你需要调用setWeekEndsAt($day)

$date->setWeekStartsAt(0);
$date->setWeekEndsAt(6);
// Or better
$date->setWeekStartsAt(Carbon::SUNDAY);
$date->setWeekEndsAt(Carbon::SATURDAY);

Try this: in AppServiceProvider::boot() method set the start and end of week like this: 试试这个:在AppServiceProvider::boot()方法中设置这样的开始和结束:

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. 只设置一周的开始到星期日将使一周只有一天。

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

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