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

oliver_s's avatar

Carbon - Monday of week (not current)

I have the following Problem. I get a date, maybe "02.07.2015" and i want to know the date of monday from this week (29.06.2015).

I know, how i can get monday of the current week, but is there a way to get the monday of another week by the given date?

0 likes
7 replies
bobbybouwmann's avatar
Level 88

Yeah you can! Carbon is pretty awesome with that

$date = Carbon::create(2015, 7, 2); // 2015-07-02

// To get the first week of the day we can do this
$monday = $date->startOfWeek(); // 2015-06-29

$mondayTwoWeeksLater = $date->addWeek(2); // 2015-07-13

Documentation: http://carbon.nesbot.com/docs/

3 likes
JosephJones's avatar

Carbon is the closest thing to magic when it comes to date manipulation!

4 likes
BENderIsGr8te's avatar

Irony...I always thought "Carbon" was a weird name for the library until the first time I googled "PHP Carbon Date", as I said it in my head I thought "Carbon Dating...the best way to calculate the age of something...what a great name".

4 likes
oliver_s's avatar

@bobbybouwmann thank you, that works great.

now i have the next problem.

I try to explain my Problem on an Example:

$date = Carbon::create(2015, 7, 2); // 2015-07-02

$date = $date->startOfWeek(); // 2015-06-29

$taskList = array();
foreach($values as $value)
{
    $taskList[...] = [
        'task_Mo' => Tasks::where('date', '=', $date)->get(),
        'task_Tu' => Tasks::where('date', '=', $date->addDays(1))->get(),
        'task_We' => Tasks::where('date', '=', $date->addDays(2))->get(),
        'task_Th' => Tasks::where('date', '=', $date->addDays(3))->get(),
        'task_Fr' => Tasks::where('date', '=', $date->addDays(4))->get()
    ];
}

if i do these, i dont get only the right day, may $date variable is set to the new day. So that my foreach get wrong values.

if i do these:

foreach($values as $value)
{
    $taskList[...] = [
        'task_Mo' => Tasks::where('date', '=', $date)->get(),
        'task_Tu' => Tasks::where('date', '=', $date->addDay())->get(),
        'task_We' => Tasks::where('date', '=', $date->addDay())->get(),
        'task_Th' => Tasks::where('date', '=', $date->addDay())->get(),
        'task_Fr' => Tasks::where('date', '=', $date->addDay())->get()
    ];
}

i get the right value for my first loop, but the problem is, that my $date variable is set new.

Is there a way to get the next days without change the value of $date?

bobbybouwmann's avatar

Well you can do this

$date = $date->startOfWeek(); // 2015-06-29

foreach($values as $value)
{
    $currentDate = $date;

    $taskList[...] = [
        'task_Mo' => Tasks::where('date', '=', $currentDate)->get(),
        'task_Tu' => Tasks::where('date', '=', $currentDate->addDay())->get(),
        'task_We' => Tasks::where('date', '=', $currentDate->addDay())->get(),
        'task_Th' => Tasks::where('date', '=', $currentDate->addDay())->get(),
        'task_Fr' => Tasks::where('date', '=', $currentDate->addDay())->get()
    ];
}

This way you always start with the set date ;)

luceos's avatar

The problem @bobbybouwmann is forgetting is that the date object is mutated when adding or substracting. What you can do is use a copy if you want to keep the original date intact.

foreach($values as $value)
{
    $taskList[...] = [
        'task_Mo' => Tasks::where('date', '=', $date)->get(),
        'task_Tu' => Tasks::where('date', '=', $date->copy()->addDays(1))->get(),
        'task_We' => Tasks::where('date', '=', $date->copy()->addDays(2))->get(),
        'task_Th' => Tasks::where('date', '=', $date->copy()->addDays(3))->get(),
        'task_Fr' => Tasks::where('date', '=', $date->copy()->addDays(4))->get()
    ];
}
2 likes
tijan's avatar

You can simply do this by:

Carbon::parse("2018-01-01")->modify("next Sunday");
1 like

Please or to participate in this conversation.