【问题标题】:carbon return number of days for a given year给定年份的碳回归天数
【发布时间】:2017-12-21 15:17:27
【问题描述】:

Carbon 可以返回给定年份的天数吗?

    $dt = Carbon::parse($year . '-'. 1 .'-' . $from);

    var_dump($dt->format('z') + 1 );

这似乎不起作用

【问题讨论】:

  • 你的意思是:一年的总天数,或者给定日期一年过去的天数? 'z' 让我假设后者,问题表明前者......
  • 另外:如果你有 int 值,为什么不使用 create(),而不是 parse()?

标签: php date php-carbon


【解决方案1】:

你可以通过使用碳属性来获得

Carbon::parse('2020-01')->daysInYear //for year

Carbon::parse('2020-01')->daysInMonth //for month

【讨论】:

    【解决方案2】:

    您可以使用comparison 来做到这一点

    echo 'Days in the year: ', ($dt->isLeapYear() ? 366 : 365);
    

    【讨论】:

      【解决方案3】:
      echo 'Days in the year: ', 365 + $dt->format('L');
      

      L:是否闰年; 1 如果是闰年,0 否则

      【讨论】:

        猜你喜欢
        • 2016-09-02
        • 2011-06-20
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-14
        • 2017-12-04
        相关资源
        最近更新 更多