Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cib88's avatar
Level 2

Getting the first and last date of the current month and past 2 months.

Hi i'm using the carbon package and i'm trying to get the first ad last date of the current month. The the first and last date of the past 2 months.

I'm pretty close to getting them but i'm not sure if it's correct way and there a problem that appears on the last day of the months.

$first_day_of_the_current_month = Carbon::now()->startOfMonth()->toDateString();
            $last_day_of_the_current_month = Carbon::now()->endOfMonth()->toDateString();

            $first_day_of_month_1 = Carbon::now()->startOfMonth()->modify('-1 month')->toDateString();
            $last_day_of_month_1 = Carbon::now()->endOfMonth()->modify('-1 month')->toDateString();

            $first_day_of_month_2 = Carbon::now()->startOfMonth()->modify('-2 months')->toDateString();
            $last_day_of_month_2 = Carbon::now()->endOfMonth()->modify('-2 months')->toDateString();

I've set these variables the when I echo them out I get the following

2018-01-01 2018-01-31

2017-12-01 2017-12-31

2017-11-01 2017-12-01

As you can see the $last_day_of_month_2 doesn't work. I Assume it's because there are 31 days in this month and only 30 in November so Carbon::now()->endOfMonth()->modify('-2 months') can't find 31st - 2 months because there isn't one. Is there any easy way to get the first and last dat of the current month and then past 2 months?

Thanks

0 likes
5 replies
tykus's avatar

If you use the first day of the month as your base for calculating the end of the month, it would produce correct results:

$first_day_of_the_current_month = Carbon::today()->startOfMonth()->toDateString();
$last_day_of_the_current_month = $first_day_of_the_current_month->copy()->endOfMonth()->toDateString();

$first_day_of_month_1 = $first_day_of_the_current_month->copy()->subMonth()->toDateString();
$last_day_of_month_1 = $first_day_of_month_1->copy()->endOfMonth()->toDateString();

$first_day_of_month_2 = $first_day_of_the_current_month->copy()->subMonth()->toDateString();
$last_day_of_month_2 = $first_day_of_month_2->copy()->endOfMonth()->toDateString();
cib88's avatar
Level 2

@tykus Yeah that kind of works I justt needed to remove the ->toDateString() otherwise got an error then needed to add a 2 in the $first_day_of_month_2 subMonth()

Thanks

tykus's avatar
tykus
Best Answer
Level 104

Sorry, let me try that again; of course $first_day_of_the_current_month, $first_day_of_month_1 and $first_day_of_month_2 all are strings as a result of toDateString() (my bad!)

Also, in the last case I should have used subMonths(2) rather than subMonth().

$first_day_of_the_current_month = Carbon::today()->startOfMonth();
$last_day_of_the_current_month = $first_day_of_the_current_month->copy()->endOfMonth();

$first_day_of_month_1 = $first_day_of_the_current_month->copy()->subMonth();
$last_day_of_month_1 = $first_day_of_month_1->copy()->endOfMonth();

$first_day_of_month_2 = $first_day_of_the_current_month->copy()->subMonths(2);
$last_day_of_month_2 = $first_day_of_month_2->copy()->endOfMonth();

Having done this, you would call toDateString() wherever you echo the variable:

{{ $first_day_of_the_current_month->toDateString() }}
1 like
cib88's avatar
Level 2

Thanks for the update I pretty much figured it out after your first comment, that helped a lot. I've updated the bet answer to the correct one though :)

BlackDiamond's avatar
//Last Month
$firstDayofPreviousMonth = Carbon::now()->startOfMonth()->subMonth()->toDateString();
$lastDayofPreviousMonth = Carbon::now()->subMonth()->endOfMonth()->toDateString();
//Last 2 Month Sales
$first_day_of_month_2 = Carbon::now()->startOfMonth()->subMonth(2)->toDateString();
$last_day_of_month_2 = Carbon::now()->subMonth(2)->endOfMonth()->toDateString();

Please or to participate in this conversation.