PHP Carbon“month()”方法生成错误的DateTime [英] PHP Carbon "month()" method generates wrong DateTime

查看:149
本文介绍了PHP Carbon“month()”方法生成错误的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是偶然发现了一些很奇怪的事情。我使用Carbon包来生成DateTime对象。



我使用以下代码为2016年9月初创建一个DateTime字符串:

  Carbon :: create() - >月(9)

如果我使用Laravel的dd()函数输出,我收到以下输出:

  Carbon\Carbon {
+date:2016-10-01 10:22:36.000000
+timezone_type:3
+timezone:欧洲/维也纳
}

它将返回10月1日,而不是9月1日!每隔一个月就可以正常工作。



我也试过这些:

  Carbon :: now() - >月(9)
(新碳) - >月(9)

但是我得到的错误结果也是一样。



有没有人会遇到这个错误,有人可以尝试一下,告诉我,如果你收到相同的输出?或者我只是做错了事情,即使我不能想到任何东西?



提前感谢

解决方案

我猜这个问题是今天是31日。



Carbon :: create() - > month(9)试图在9月份的同一天。由于9月份没有31日,10月1日返回。尝试:

  Carbon :: create() - > day(1) - > month(9); 

  Carbon :: create() - > startOfMonth() - > month(9); 

  Carbon :: create(null,9); 


I just stumbled upon something really weird. I use the "Carbon" package to generate DateTime objects.

I use the following code to create a DateTime string for the beginning of September 2016:

Carbon::create()->month(9)

If I output this using Laravel's "dd()" function, I receive the following output:

Carbon\Carbon {
  +"date": "2016-10-01 10:22:36.000000"
  +"timezone_type": 3
  +"timezone": "Europe/Vienna"
}

It returns the 1st of October, rather than the 1st of September! It works fine with every other month.

Ive also tried these:

Carbon::now()->month(9)
(new Carbon)->month(9)

But I get the same wrong result.

Does anyone else experience this bug or can someone please try this out and tell me if you receive the same output? Or am I just doing something wrong, even though I can't think of anything?

Thanks in advance.

解决方案

I guess that the problem is todays date which is the 31st.

Carbon::create()->month(9) tries to take the same day for September. Since there is no 31st in September, it returns October 1st. Try:

Carbon::create()->day(1)->month(9);

Or

Carbon::create()->startOfMonth()->month(9);

Or

Carbon::create(null, 9);

这篇关于PHP Carbon“month()”方法生成错误的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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