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

Похожие статьи
Development with PHP
Arrays in PHP
Date and Time in PHP - basics
How to display time of multiple timezones in PHP
How to add variable to url in PHP
json_decode
How to get screen size with PHP
Call function from another file
Premature end of chunk coded message body: closing chunk expected
Generate unique random numbers with PHP
Check your HTTP_USER_AGENT
Compare two dates
OOP
Namespaces in PHP
Comments in PHP
Cookies
PHP sessions
Authentication and Authorization in PHP 8
PHP linters we use
PHP errors: case studies
card_from_db(): banner card from a DB row
PHP versions: history in brief
Creating rules in .htaccess
require, require_once, include, include_once
Flat pair arrays: three pitfalls from the Related articles block
Deduplication in PHP
PHP course: zero to hero in 50 lessons

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: