Laravel Carbon get start + end of current week

Steve Brown picture Steve Brown · Apr 19, 2016 · Viewed 32.4k times · Source

I am working with Laravel 4 on a tool to publish/schedule restaurant menus on facebook. For this I need a date selector for the current week, starting always on monday and ending always on sunday.

Wireframe for restaurant menu

I'have played around with the examples http://carbon.nesbot.com/docs/#api-getters but without success.

Any idea?

Answer

Its_aggarwal picture Its_aggarwal · Aug 26, 2018

This is pretty simple with Carbon Library. Here is the code example:

$now = Carbon::now();
$weekStartDate = $now->startOfWeek()->format('Y-m-d H:i');
$weekEndDate = $now->endOfWeek()->format('Y-m-d H:i');

Even you have the option to change start and end day of the week. It is like this,

$start = $now->startOfWeek(Carbon::TUESDAY);
$end = $now->endOfWeek(Carbon::MONDAY);

Source: https://carbon.nesbot.com/docs/#api-getters