62

I am using carbon but trying to get the first day of the month so I can run a report from the beginning of the month till the current day.

    $date = [
        'start' => new \Carbon\Carbon('last month'),
        'end' => new \Carbon\Carbon('today')
    ];

The above code will show todays date back to same date in the previous month. But I want to get from the 1st to now.

Is there an easy way to do this like I am above? Cant find anything in the docs.

Lovelock
  • 7,689
  • 19
  • 86
  • 186

3 Answers3

161

You can use following function

$start = Carbon::now()->startOfMonth();
$end = Carbon::now();
Shriganesh Shintre
  • 2,428
  • 3
  • 17
  • 16
  • 19
    Just a reminder, if you want time as `00:00:00`, this is the perfect answer. otherwise, @Narendrasingh 's answer is more friendly :) – Jinzhao Huo Jun 26 '17 at 13:18
106

Try as

$start = new Carbon('first day of this month');

CARBON DOCS Refer #Testing Aids

If you already have Carbon object and want to find first day of month for that object you can try as,

$startDate = Carbon::now(); //returns current day
$firstDay = $startDate->firstOfMonth();  
Kusal Dissanayake
  • 714
  • 14
  • 32
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
1

Example:

Carbon::now()->format('Y-m-01')
// 2023-02-01

Carbon::now()->format('Y-m-t')
// 2023-02-28
Ying-Shan Lin
  • 607
  • 5
  • 22