lewis4u
lewis4u

Reputation: 15037

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?

Upvotes: 67

Views: 170478

Answers (9)

vijay saini
vijay saini

Reputation: 441

use Carbon\Carbon;

$today= Carbon::now();

echo $today->year;
echo $today->month;
echo $today->day;
echo $today->hour;
echo $today->minute;
echo $today->second;

Upvotes: 1

ali hassan
ali hassan

Reputation: 449

Laravel 8

return date('m'); 

or

return now()->format('m');

Upvotes: 1

user2693928
user2693928

Reputation:

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

Update:

even this works since Laravel 5.5^

echo now()->month

Upvotes: 123

ahinkle
ahinkle

Reputation: 2261

w/ Carbon + Laravel:

now()->format('M')

Upvotes: 3

iainn
iainn

Reputation: 17417

I think you've already worked this out in a comment, but just for clarity: Carbon extends PHP's native DateTime class, so you can use any of the methods available on that, like format:

Carbon::now()->format('M');

(where M is the modifier for A short textual representation of a month, three letters)

Upvotes: 25

Mehrdad Dehghani
Mehrdad Dehghani

Reputation: 677

I wanted to get the current month and got to this question, to get the current month:

$now = Carbon::now();
$monthStart = $now->startOfMonth();

Upvotes: 3

Sumon Sarker
Sumon Sarker

Reputation: 129

Just use this in your any blade file for print year:

{{ \Carbon\Carbon::now()->year }}  

Upvotes: 9

Rojith Peiris
Rojith Peiris

Reputation: 201

You cant call it statically use

$now = Carbon::now();

echo $now->month

Upvotes: 0

Gayashan Perera
Gayashan Perera

Reputation: 671

You can use these both ways to get the current month

Carbon::now()->month;

or

Carbon::now()->format('m');

Upvotes: 18

Related Questions