php Carbon::now() - 仅一个月
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40661011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Carbon::now() - only month
提问by lewis4u
I couldn't find anywhere in documentationhow to show current year or month with Carbon?
我在文档中找不到如何使用 Carbon 显示当前年份或月份?
when i write this:
当我写这个:
Carbon\Carbon::now('m');
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?
我怎样才能做到这一点?
回答by user2693928
$now = Carbon::now();
echo $now->year;
echo $now->month;
echo $now->weekOfYear;
回答by iainn
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 扩展了PHP 的本机DateTime
类,因此您可以使用任何可用的方法,例如format
:
Carbon::now()->format('M');
(where M
is the modifier for A short textual representation of a month, three letters)
( A short textual representation of a month,三个字母M
的修饰语在哪里)
回答by Gayashan Perera
You can use these both ways to get the current month
您可以使用这两种方式来获取当前月份
Carbon::now()->month;
or
或者
Carbon::now()->format('m');
回答by Sumon Sarker
Just use this in your any blade file for print year:
只需在打印年份的任何刀片文件中使用它:
{{ \Carbon\Carbon::now()->year }}
回答by Mehrdad Dehghani
I wanted to get the current month and got to this question, to get the current month:
我想得到当前月份并得到这个问题,得到当前月份:
$now = Carbon::now();
$monthStart = $now->startOfMonth();
回答by ahinkle
w/ Carbon + Laravel:
带碳 + Laravel:
now()->format('M')
回答by Rojith Randula Peiris
You cant call it statically use
你不能称之为静态使用
$now = Carbon::now();
$now = Carbon::now();
echo $now->month
回声 $now->month