具有时间偏移量的Carbon :: now()导致不同的行为 [英] Carbon::now() with time-offset result in different behaviors

查看:512
本文介绍了具有时间偏移量的Carbon :: now()导致不同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Carbon来操纵laravel项目中的日期.

I'am using Carbon for manipulating dates in a laravel project.

Carbon::now('+5:30');

以上代码在本地环境中运行良好,但在开发环境中运行不正常.

Above code is working fine in local environment but not in development environment.

这就是我在dd(Carbon::now('+5:30'));

1-本地环境 php版本-5.6.3

2-开发环境中 php版本-5.5.9-1ubuntu4.14

但是如果我使用时区名称而不是像这样的时间偏移,则两种环境的行为都一样

But both environment behaves same if i use timezone name instead of time-offset like,

 Carbon::now('Asia/Tokyo');

这与 php版本有关吗?

推荐答案

搜索相关问题后,我为您提供了答案.

I have an answer for you after searching for related issues.

PHP 5.5.9版似乎有一个错误:

It seems that PHP version 5.5.9 had a bug:

https://stackoverflow.com/a/14069062/5912664

因此您不能在Carbon上使用该方法,但是以下方法应该起作用:

So you can't use that method with Carbon, but the following should work:

Carbon::now()->addHours(5)->addMinutes(30);

您可以在其中放置服务器时区,以提高准确性:

You can place your servers timezone in there for added accuracy:

Carbon::now(date_default_timezone_get())->addHours(5)->addMinutes(30);

这篇关于具有时间偏移量的Carbon :: now()导致不同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆