Laravel Carbon get start + end of current week
Asked Answered
L

5

26

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?

Lubalubba answered 19/4, 2016 at 13:9 Comment(0)
C
63

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

Changeup answered 26/8, 2018 at 16:8 Comment(3)
this will set both variables to the end of the week, as ->startOfWeek will change the variable, not make clone of itFussbudget
To avoid making a clone, use ->copy()Relativize
Better to use the interface for Carbon constants: CarbonInterface::MONDAYPurnell
F
8

The best way is using jquery plugin

http://api.jqueryui.com/datepicker/

In your view.blade.php make input field

<input type="text" id="in">

In your script file select this input and set date range

<script>
        $("#in").datepicker({
            minDate: new Date("{{Carbon\Carbon::now()->startOfWeek()->format('Y/m/d')}}"),
            maxDate: new Date("{{Carbon\Carbon::now()->endOfWeek()->format('Y/m/d')}}")
        });
</script>

This should be look like this

https://i.sstatic.net/bXZkT.jpg

Fibrillation answered 20/4, 2016 at 7:55 Comment(0)
F
6

Please note, you will need to use CarbonImmutable if you want the Start Date to remain the beginning of the week. (Also it isn't "Laravel Carbon" it is just Carbon)

$now = CarbonImmutable::now();
$weekStartDate = $now->startOfWeek();
$weekEndDate = $now->endOfWeek();
Fourgon answered 15/10, 2020 at 20:33 Comment(1)
Thank you. I was totally perplexed as to why my start date was being altered by calling endOfWeek() on the original date instance. Now I get it.Arabella
S
5

This gives you the start of the week (monday) until the end of the week (sunday).

No idea whether this is a setting on the server. (Some people put the initial week on Sunday)

private $start;
private $end;

public function setWeekPeriod($weeknumber)
{
    $week_start = (new DateTime())->setISODate(date("Y"),$weeknumber)->format("Y-m-d H:i:s");

    $this->start = Carbon::createFromFormat("Y-m-d H:i:s", $week_start);
    $this->start->hour(0)->minute(0)->second(0);
    $this->end = $this->start->copy()->endOfWeek();
}
Selfdetermination answered 19/4, 2016 at 13:16 Comment(0)
V
3

Worked fine for me.

$now = now();
$weekStartDate = $now->copy()->startOfWeek()->format('Y-m-d');
$weekEndDate = $now->copy()->endOfWeek()->format('Y-m-d');
Voyageur answered 28/11, 2022 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.