Laravel 5.3 - Carbon 日期 - UTC 偏移量获取时区名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43217426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 - Carbon Date - UTC offset get timezone name
提问by Colton Wagner
I am trying to get a timezone name from a UTC offset in Laravel 5.3 using Carbon. Code listed below any help would be much appreciated.
我正在尝试使用 Carbon 从 Laravel 5.3 中的 UTC 偏移量中获取时区名称。下面列出的任何帮助的代码将不胜感激。
/* current code iteration */
$utcOffset = -5;
$timezone = Carbon::now($utcOffset)->timezone->getName();
echo $timezone;
// Result: -05:00
// Expected Result: EST
/* tried code */
$timezone = Carbon::now($utcOffset)->tzName;
// Result: -05:00
/* What I used prior to Carbon */
$timezone = timezone_name_from_abbr(null, $utcOffset * 3600, TRUE);
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($timezone));
$timezone = $dateTime->format('T');'
What am I missing? I feel daft..
我错过了什么?我觉得很蠢..
采纳答案by Colton Wagner
Tried updating Carbon to no evail ended up using the old datetime class.
尝试将 Carbon 更新为 no eail 最终使用旧的 datetime 类。
$timezone = timezone_name_from_abbr(null, $utcOffset * 3600, TRUE);
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone($timezone));
$timezone = $dateTime->format('T');
回答by DevK
This works for me:
这对我有用:
$now = Carbon::now(-5);
echo $now->timezone;
// prints 'America/Chicago'
回答by Yevgeniy Afanasyev
In a new Carbon
it is timezoneName
property;
在新的意义上,Carbon
它是timezoneName
财产;
$now = Carbon::now(-5);
echo $now->timezoneName;
//or
echo $now->timezone->getName();
回答by A.G.
Preface:
前言:
The accepted answer works in most cases but as mentioned in the user contributed notes area of timezone_name_from_abbr(), there are issues with using the function, like returning false instead of actual timezoneand returning a "historical" (i.e. deprecated) timezone identifier rather than the current standard one for a given location. Which are still valid to this date.
接受的答案在大多数情况下都有效,但如timezone_name_from_abbr()的用户贡献注释区域所述,使用该函数存在问题,例如返回 false 而不是实际时区并返回“历史”(即已弃用)时区标识符而不是给定位置的当前标准。至今仍有效。
Also, the original code returns the value as expected, as long as you know that as per Carbon docs, if you look at https://carbon.nesbot.com/docs/#api-timezone
此外,如果您查看https://carbon.nesbot.com/docs/#api-timezone,原始代码会按预期返回值,只要您知道根据 Carbon 文档
the original name of the timezone (can be region name or offset string):
时区的原始名称(可以是区域名称或偏移字符串):
One more thing to note here is that, it is considered not reliable to derive timezone off of offset value as it does not take into consideration the DST observed periods offset.
这里要注意的另一件事是,从偏移值导出时区被认为是不可靠的,因为它没有考虑 DST 观察到的周期偏移。
So, this all to actually say that deriving timezone off of offset is not always possible.
所以,这一切实际上是说从偏移量导出时区并不总是可行的。
Answer:
回答:
But since the OP mentioned Carbon and timezone based on offset, as per the Carbon docs as of now, the answer should be
但是由于 OP 提到了基于偏移量的碳和时区,根据目前的碳文档,答案应该是
$date = Carbon::now('-5');
echo $date->tzName;