carbon return number of days for a given year
Asked Answered
P

3

6

Can Carbon return number of days for a given year?

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

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

this does not seems to work

Planetesimal answered 17/7, 2017 at 7:25 Comment(2)
What do you mean: The total number of days a year has, or the number of days passed in the year for a given date? 'z' lets me assume the latter, the question suggests the former...Mecke
Also: Why don't you use create(), instaed of parse(), if you have the int-values?Mecke
S
6
echo 'Days in the year: ', 365 + $dt->format('L');

L: Whether it's a leap year; 1 if it is a leap year, 0 otherwise

Samos answered 27/7, 2017 at 13:57 Comment(0)
P
5

You can get by using carbon property

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

Carbon::parse('2020-01')->daysInMonth //for month
Punchdrunk answered 12/4, 2021 at 7:29 Comment(1)
yours should be the best answer ^_^ .... .... now()->daysInYearNgocnguyen
H
2

You can do this by using a comparison

echo 'Days in the year: ', ($dt->isLeapYear() ? 366 : 365);
Highspirited answered 31/1, 2018 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.