Date and Time in PHP
| Introduction | |
| Example without OOP | |
| DateTimeImmutable: OOP example | |
| UTC time in PHP | |
| Week number in PHP | |
| Related Articles |
Introduction
Time and date are usually handled with a function
date
In the OOP paradigm you can use
DateTimeImmutable
Examples
Today date - numbers only
date("Y-m-d"); date("Y:m:d");
2026-10-12
2026:10:12
Today date - month spelled out
date("Y-M-d");
date("Y:M:d");
2026-Oct-12
2026:Oct:12
Weekday spelled out
date("D");
Mon
Weekday plus date
date("D-Y-M-d");
date("D:Y:M:d");
date("D Y:M:d");
Mon-2026-Oct-12
Mon:2026:Oct:12
Mon 2026:Oct:12
Month and day plus time
date("M d H:i:s");
Oct 12 15:21:23
Current time in the format Epoch Time
time();
1791807683
DateTimeImmutable: OOP example
If you know OOP a little you can create a DateTimeImmutable object and use it
$now = new DateTimeImmutable('now', new DateTimeZone('Europe/Moscow')); echo'Moscow time: ' echo $now->format('H:i:s');
Moscow time: 15:21:23
UTC time in PHP
$utc = new DateTimeImmutable('now', new DateTimeZone('UTC')); echo 'UTC: ' echo $utc->format('H:i:s');
UTC
12:21:23
Week number in PHP
In some countries, for example in
Finland
planning is based on the week number.
Find out the current week number with the following code
$now = new DateTimeImmutable('now', new DateTimeZone('Europe/Moscow')); echo'Current week number: ' echo $now->format('W');
Current week number: 42
Article author: Andrei Olegovich