Get the 15th of the month with Carbon

Posted 3 months ago by timgavin

I'm configuring pay periods, which are on the 15 of the month and the last day of the month.

I want to grab sales that happened between those days, so I need to find sales that happened between the 1st and the 15th, and the 16th and the end of the month.

The first part is easy. The second, not so hard, but a little on the messy side.

$month = Carbon::now()->format('m');
$year = Carbon::now()->format('y');
$day = $year.'-'.$month.'-15';

$startDate = Carbon::parse($day)->format('Y-m-d');
$endDate = Carbon::now()->endOfMonth()->format('Y-m-d');

Is there a cleaner way of doing this?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.