Get Monday of week with DateTime

Get Monday of week with DateTime

Working with calendars in PHP offers interesting problems. One of them I stumbled upon was finding the Monday of a specific week as a DateTime object.

There are multiple ways, especially with adding intervals and getting the current day of the week, but the easiest way I found was the following:

$mondayOfCurrentWeek = (new \DateTime())->setISODate($year, $week);

When you want a timestamp back you have to remember to set the time as the current one is used by default. You can do this with setTime.

$timestamp = (new \DateTime())->setISODate($year, $week)
    ->setTime(0, 0, 0)