PHP Date and Time
Almost every real app needs to show, store, or calculate dates: "posted 3 days ago", invoice due dates,
scheduling, log timestamps. PHP gives you two ways to work with dates — the classic date()
function and the object-oriented DateTime class. This page covers both, plus strtotime()
and timezones.
1) The date() Function
date() formats the current time (or a given Unix timestamp) into a string, using format
characters that each stand for a piece of the date.
<?php
echo date('Y-m-d'); // e.g. 2026-08-14
echo date('H:i:s'); // e.g. 14:05:09
echo date('l, d F Y'); // e.g. Friday, 14 August 2026
?>
| Character | Meaning | Example |
|---|---|---|
Y | 4-digit year | 2026 |
y | 2-digit year | 26 |
m | Month, zero-padded | 08 |
d | Day of month, zero-padded | 14 |
l | Full weekday name | Friday |
D | Short weekday name | Fri |
F | Full month name | August |
H | 24-hour, zero-padded | 14 |
g | 12-hour, no padding | 2 |
i | Minutes, zero-padded | 05 |
s | Seconds, zero-padded | 09 |
A | AM/PM (uppercase) | PM |
2) Live Demo: The Server's Real Current Time
The values below are computed with date() the instant this page renders on the server —
reload the page and the seconds will change.
| Format string | Live output right now |
|---|---|
Y-m-d | 2026-10-27 |
Y-m-d H:i:s | 2026-10-27 10:50:55 |
l, d F Y | Tuesday, 27 October 2026 |
D M j, Y | Tue Oct 27, 2026 |
g:i A | 10:50 AM |
H:i:s | 10:50:55 |
3) time() — The Current Unix Timestamp
time() returns the number of seconds since 1 January 1970 (the "Unix epoch"). It's the raw number that date() formats.
<?php
$now = time();
echo $now; // e.g. 1786723200
echo date('Y-m-d', $now); // format that timestamp
?>
time() on this server returns 1793098255.4) strtotime() — Parsing Human-Readable Strings
strtotime() converts almost any English date phrase into a Unix timestamp, which you can then
format with date().
<?php
echo date('Y-m-d', strtotime('next monday'));
echo date('Y-m-d', strtotime('+1 week'));
echo date('Y-m-d', strtotime('-3 days'));
echo date('Y-m-d', strtotime('first day of next month'));
?>
| strtotime() phrase | Live result |
|---|---|
next monday | 2026-11-02 (Monday) |
+1 week | 2026-11-03 |
-3 days | 2026-10-24 |
first day of next month | 2026-11-01 |
5) The DateTime Class (Object-Oriented)
For anything more than simple formatting, the DateTime class is easier to work with — it lets
you modify, compare, and diff dates as objects instead of juggling timestamps.
<?php
$date = new DateTime(); // now
echo $date->format('Y-m-d H:i:s');
$date->modify('+10 days');
echo $date->format('Y-m-d'); // 10 days from now
?>
6) Setting a Timezone
All date/time functions use a default timezone. Set it once, near the top of your app, with
date_default_timezone_set(). This exact site does that already in track-visitor.php:
<?php
date_default_timezone_set("Asia/Kolkata");
?>
track-visitor.php, included in this page's <head>), every timestamp on this page is genuinely in Indian Standard Time — not UTC.7) Calculating a Difference with DateTime::diff()
DateTime::diff() compares two DateTime objects and returns a DateInterval
with years/months/days/hours between them — much safer than subtracting timestamps by hand.
<?php
$today = new DateTime('today');
$newYear = new DateTime('first day of January next year');
$diff = $today->diff($newYear);
echo $diff->days . ' days until New Year';
?>
| Calculation | Live result |
|---|---|
| Days until next New Year's Day (from today, 2026-10-27) | 66 days (target: 2027-01-01) |
| Days since 2024-01-01 | 1030 days |
date()/strtotime() for quick, one-off formatting;
switch to the DateTime class once you need to modify, compare, or diff dates — the code
stays far more readable.