PHP
date handling
weekend calculation
programming tutorial
web development

PHP How to get Sunday and Saturday given a date input?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Given any input date, the usual goal is to find the Saturday and Sunday that belong to that same calendar week. In PHP, the cleanest way to do this is with DateTimeImmutable, because it avoids accidental mutation while you calculate relative days.

Decide What "The Week" Means

Before writing code, be clear about the rule:

  • does the week run Sunday through Saturday
  • or Monday through Sunday
  • and do you want the weekend before or after the given date

Most "get Saturday and Sunday for this date" questions mean: find the Sunday and Saturday of the week containing the date, assuming Sunday starts the week.

Using DateTimeImmutable

Here is a simple function that returns both weekend days for the week containing the input date:

php
1<?php
2
3function getWeekendForDate(string $input): array
4{
5    $date = new DateTimeImmutable($input);
6    $dayOfWeek = (int) $date->format('w'); // 0 = Sunday, 6 = Saturday
7
8    $sunday = $date->modify("-{$dayOfWeek} days");
9    $saturday = $sunday->modify('+6 days');
10
11    return [
12        'sunday' => $sunday->format('Y-m-d'),
13        'saturday' => $saturday->format('Y-m-d'),
14    ];
15}
16
17print_r(getWeekendForDate('2026-03-11'));

If the input date is a Wednesday, this moves backward to that week's Sunday and forward to that week's Saturday.

Why format('w') Helps

format('w') returns:

  • '0 for Sunday'
  • '1 for Monday'
  • '...'
  • '6 for Saturday'

That means the number itself tells you how many days you need to subtract to reach Sunday.

Once you have Sunday, Saturday is always six days later in the same week model.

Alternative With Relative Strings

PHP's relative date parser can also express the same logic, but it can be less predictable if you are not careful about what counts as "this" week.

php
1<?php
2
3$date = new DateTimeImmutable('2026-03-11');
4$sunday = $date->modify('last sunday');
5$saturday = $sunday->modify('+6 days');
6
7echo $sunday->format('Y-m-d') . PHP_EOL;
8echo $saturday->format('Y-m-d') . PHP_EOL;

This can work, but relative phrases such as last sunday and next saturday are easier to misread than using the numeric weekday directly.

For predictable application logic, the format('w') approach is usually clearer.

If Your Week Starts on Monday

If your business rules treat Monday as the first day of the week, use format('N'), where Monday is 1 and Sunday is 7, then derive the relevant weekend differently.

php
1<?php
2
3function getWeekendMondayWeek(string $input): array
4{
5    $date = new DateTimeImmutable($input);
6    $dayOfWeek = (int) $date->format('N'); // 1 = Monday, 7 = Sunday
7
8    $monday = $date->modify('-' . ($dayOfWeek - 1) . ' days');
9    $saturday = $monday->modify('+5 days');
10    $sunday = $monday->modify('+6 days');
11
12    return [
13        'saturday' => $saturday->format('Y-m-d'),
14        'sunday' => $sunday->format('Y-m-d'),
15    ];
16}

This is a different week definition, so do not mix the two models accidentally.

Common Pitfalls

The most common mistake is not defining what "the week" means. Sunday-start and Monday-start calendars produce different answers for the same input date near week boundaries.

Another issue is mutating a DateTime object repeatedly and then being surprised that later calculations use the already-modified value. DateTimeImmutable avoids that confusion.

A third pitfall is relying on English relative date strings too heavily. They are convenient, but numeric weekday logic is usually easier to test and reason about.

Finally, if the date comes from user input with time-zone context, parse it consistently before calculating the weekend. A date near midnight in different time zones can land in different calendar days.

Summary

  • Use DateTimeImmutable to calculate weekend dates safely from an input date.
  • 'format('w') is a simple way to find the Sunday-start week containing the date.'
  • Once you have Sunday, Saturday is six days later.
  • Be explicit about whether your application treats Sunday or Monday as the start of the week.
  • Prefer clear numeric weekday logic over ambiguous relative date strings for production code.

Course illustration
Course illustration

All Rights Reserved.