我正在使用carbon,但试图获取该月的第一天,这样我就可以运行从月初到当天的报告。
$date = [
'start' => new \Carbon\Carbon('last month'),
'end' => new \Carbon\Carbon('today')
];
上面的代码将显示今天的日期到上个月的同一日期。但我想从一开始到现在。
有像我上面那样简单的方法吗?在文档中找不到任何内容。
您可以使用以下功能
$start = Carbon::now()->startOfMonth();
$end = Carbon::now();
尝试作为
$start = new Carbon('first day of this month');
如果您已经有
Carbon
对象并想查找该对象的第一天,您可以尝试,
$startDate = Carbon::now(); //returns current day
$firstDay = $startDate->firstOfMonth();
示例:
Carbon::now()->format('Y-m-01')
// 2023-02-01
Carbon::now()->format('Y-m-t')
// 2023-02-28
Carbon::now()->format('Y-m-01')
这对我有帮助,因为它不像其他版本那么抽象。我需要拿
Y-m-02
。