Dates and time

Содержание
time and date
Format with DateTimeImmutable
Modify dates
Compare two dates

time and date

Function time returns the current moment as a whole number. Function date formats that moment with a pattern like Y-m-d.

Start with the guide Date and Time in PHP for the full list of patterns.

echo time(); echo date("Y-m-d H:i:s");

Format with DateTimeImmutable

DateTimeImmutable keeps a fixed point in time and never changes itself. Call format with a pattern to render year, month, day and time.

Set the zone explicitly so results stay stable on any server. See also timezones for zone names.

$now = new DateTimeImmutable("now", new DateTimeZone("UTC")); echo $now->format("Y-m-d H:i:s");

Modify dates

The modify method returns a new object and leaves the original unchanged.

Pass expressions like +3 days or next Monday to move forward. Chain format after modify to display the result.

$now = new DateTimeImmutable("2026-10-05", new DateTimeZone("UTC")); $later = $now->modify("+3 days"); echo $later->format("Y-m-d");

Compare two dates

Compare objects directly to find the earlier date. You can also compare timestamps when you need numbers.

The tutorial Compare two dates shows more cases step by step.

$a = new DateTimeImmutable("2026-10-05"); $b = new DateTimeImmutable("2026-10-06"); var_dump($a < $b); var_dump($a->getTimestamp() < $b->getTimestamp());

Next: JSON in PHP

Автор статьи: Arthur Isaev

Похожие статьи
Программирование на PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Изображение баннера

Поиск по сайту

Канал @aofeed Чат @aofeedchat

Контакты и сотрудничество:
Рекомендую наш хостинг beget.ru
Пишите на info@urn.su если Вы:
1. Хотите написать статью для нашего сайта или перевести статью на свой родной язык.
2. Хотите разместить на сайте рекламу, подходящую по тематике.
3. Реклама на моём сайте имеет максимальный уровень цензуры. Если Вы увидели рекламный блок недопустимый для просмотра детьми школьного возраста, вызывающий шок или вводящий в заблуждение - пожалуйста свяжитесь с нами по электронной почте
4. Нашли на сайте ошибку, неточности, баг и т.д. ... .......
5. Статьи можно расшарить в соцсетях, нажав на иконку сети: