How to get the day of the week
Short answer
The N format returns the day number: 1 is Monday, 7 is Sunday:
<?php
$date = new DateTimeImmutable('2025-01-30');
echo $date->format('N');Output:
4The name
The l format gives the English name:
<?php
echo (new DateTimeImmutable('2025-01-30'))->format('l');Output:
ThursdayIn another language
Take the name from your own array by the day number:
<?php
$days = [1 => 'понедельник', 'вторник', 'среда', 'четверг', 'пятница', 'суббота', 'воскресенье'];
echo $days[(int) (new DateTimeImmutable('2025-01-30'))->format('N')];Output:
четвергIs it a weekend
Saturday and Sunday have the numbers 6 and 7:
<?php
var_dump((new DateTimeImmutable('2025-02-01'))->format('N') >= 6);Output:
bool(true)Functions used in this recipe
- date — Formats a date and time.
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.