简体   繁体   English

如何使用PHP的Carbon将日期设置为本月的第n天?

[英]How to set date to nth day of this month when using Carbon of PHP?

From the document page of Carbon library , it is stated that it is possible to use simple words to initialize a Carbon object. Carbon库文档页面可以看出,可以使用简单的单词来初始化Carbon对象。 For example, Carbon::parse('first day of December 2008') or new Carbon('first day of December 2008') 例如, Carbon::parse('first day of December 2008')new Carbon('first day of December 2008')

However, when I am trying to set the date to 10th of this month by the following trials 但是,当我试图通过以下试验将日期设定为本月10日时

Carbon::parse('10th day of this month')
Carbon::parse('tenth day of this month')
Carbon::parse('10th of this month')
Carbon::parse('tenth of this month')
Carbon::parse('10 of this month')
Carbon::parse('ten of this month')

All of these fails. 所有这些都失败了。

Currently I am able to create my desired Carbon object by Carbon::parse('first day of this month')->addDays(9) , but the readability is not good. 目前我能够通过Carbon::parse('first day of this month')->addDays(9)创建我想要的Carbon对象,但是可读性不好。 More code is needed to be read and it is very easy to mistaken as 9th of this month at the first glaze. 需要阅读更多代码,很容易在本月9日的第一个釉中被误认为是错误的。

So, is there a way I can create the Carbon object with one parse only? 那么,有没有办法只用一个解析创建Carbon对象?

这有点难看,但这应该有效

Carbon::parse('10th ' . date('M'));

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

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