Nearly every real application needs to show or store a date somewhere — when a post was published, when an account was created. PHP has a quick functional way to do this, and a more capable object-oriented one.
<?php
echo date("Y-m-d"); // "2026-07-30"
echo date("d/m/Y"); // "30/07/2026"
echo date("l, F j, Y"); // "Thursday, July 30, 2026"
echo date("H:i:s"); // "14:30:00"
?>| Character | Means |
|---|---|
| Y | 4-digit year |
| m | 2-digit month |
| d | 2-digit day |
| H:i:s | Hour:minute:second, 24-hour |
| l | Full day name |
| F | Full month name |
The full set of format characters is in the date() reference on php.net — there are dozens, covering every format you're likely to need.
For anything beyond simple formatting — comparing dates, adding time, working across time zones — the object-oriented DateTime class is the better tool:
<?php
$today = new DateTime();
$deadline = new DateTime("2026-12-31");
$interval = $today->diff($deadline);
echo $interval->days . " days remaining"; // e.g. "154 days remaining"
$today->modify("+7 days");
echo $today->format("Y-m-d"); // one week from today
?>Time zones are the server's, by default
PHP's date and time functions default to the server's configured time zone, which may not match a visitor's own. For anything where that matters — a scheduled event, a countdown — either set the time zone explicitly with date_default_timezone_set(), or handle the conversion in JavaScript on the visitor's own device.