Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon::now() - only month

I couldn't find anywhere in documentation how to show current year or month with Carbon?

when i write this:

Carbon\Carbon::now('m');

it gives me the whole time stamp, but I just need the month

like

date('m'); 

but it must be Carbon!

How can I achieve this?

like image 406
lewis4u Avatar asked Nov 17 '16 17:11

lewis4u


People also ask

How do you find the current month in carbon?

$now = Carbon\Carbon::now(); $month = $now->format('m'); Assuming the current month was January, you would receive '01' from that format call. As an aside, if that's all you're using Carbon for in that class, you could just use the default PHP date() method if you feel like using less dependencies.

What does carbon :: now () return?

Carbon::now returns the current date and time and Carbon:today returns the current date. This is a sample output.

How can I get only year from carbon laravel?

Get year , month and day from timestamp date using carbon library in Laravel. $timestemp = "2020-01-01 01:02:03"; $year = Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $timestemp)->year; Get Month.


1 Answers

$now = Carbon::now(); echo $now->year; echo $now->month; echo $now->weekOfYear; 

Update:

even this works since Laravel 5.5^

echo now()->month 
like image 109
user2693928 Avatar answered Oct 20 '22 02:10

user2693928