Carbon::now() - 只有一个月

Carbon::now() - only month

我找不到任何地方 documentation 如何用 Carbon 显示当前年份或月份?

当我写这篇文章时:

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

它给了我整个时间戳,但我只需要月份

喜欢

date('m');

但必须是Carbon!

我怎样才能做到这一点?

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

更新:

自从 Laravel 5.5^

echo now()->month

我想你已经在评论中解决了这个问题,但为了清楚起见:Carbon extends PHP's native DateTime class, so you can use any of the methods available on that, like format:

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

(其中 M 月份的简短文本表示,三个字母 的修饰符)

您可以使用这两种方式来获取当前月份

Carbon::now()->month;

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

你不能静态调用它 使用

$now = Carbon::now();

echo $now->month

只需在打印年份的任何 blade 文件中使用它:

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

我想获取当前月份并得到这个问题,获取当前月份:

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

w/ 碳 + Laravel:

now()->format('M')

Laravel 8

return date('m'); 

return now()->format('m');
use Carbon\Carbon;

$today= Carbon::now();

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