Jadasdas
Jadasdas

Reputation: 879

Get sunday of last week on the month with Carbon

How I can get sunday of last week on the month with Carbon?

$startDate = Carbon::now()->endOfMonth();
$lastSunday = $startDate()->endOfWeek();

Not working. Thanks.

Upvotes: 2

Views: 4505

Answers (2)

Clément Baconnier
Clément Baconnier

Reputation: 6058

An alternative way to find the last sunday of any month can be done like this:

Carbon::parse('2024-10-16')->lastOfMonth(Carbon::SUNDAY);
    // 2024-10-27 00:00:00.0 UTC (+00:00)

Carbon::parse('2025-08-15')->lastOfMonth(Carbon::SUNDAY);
    // 2025-08-31 00:00:00.0 UTC (+00:00)

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

You can use Carbon like this:

Carbon::parse('last sunday of this month')

This will return Carbon instance with the last Sunday of current month.

Upvotes: 3

Related Questions