Encodera

Thanks for reading!

Donate to the site — we work hard for you.

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:

4

The name

The l format gives the English name:

<?php
 
echo (new DateTimeImmutable('2025-01-30'))->format('l');

Output:

Thursday

In 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.

Thanks for reading!

Donate to the site — we work hard for you.