【问题标题】:How to set date to nth day of this month when using Carbon of PHP?使用PHP的Carbon时如何将日期设置为本月的第n天?
【发布时间】:2016-09-09 10:09:12
【问题描述】:

document page of Carbon library 中可以看出,可以使用简单的词来初始化 Carbon 对象。例如,Carbon::parse('first day of December 2008')new Carbon('first day of December 2008')

但是,当我试图通过以下试验将日期设置为本月 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')

所有这些都失败了。

目前我可以通过Carbon::parse('first day of this month')->addDays(9)创建我想要的Carbon对象,但是可读性不好。需要阅读更多代码,并且很容易在第一次釉时误认为本月9日。

那么,有没有一种方法可以只用一次解析来创建 Carbon 对象?

【问题讨论】:

    标签: php date datetime timestamp php-carbon


    【解决方案1】:

    这有点难看,但这应该可以工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 2017-07-06
      • 1970-01-01
      • 2015-09-09
      • 2021-01-24
      • 2022-01-25
      • 1970-01-01
      相关资源
      最近更新 更多