Tutorials › PHP Tutorial › PHP Date and Time

PHP Date and Time

14 min read · Lesson 19 of 24

Dates are everywhere on a website: the year in the footer, when a blog post was published, a payment deadline, how many days are left before an exam. PHP has two toolkits for this: the classic date()/strtotime() functions, and the object-oriented DateTime classes.

Set your time zone first

PHP needs to know which time zone you mean, otherwise it uses the server's default (often UTC, which is three hours behind Kenya). Set it once at the start of your app:

<?php
date_default_timezone_set("Africa/Nairobi");

You can also set date.timezone = Africa/Nairobi in php.ini so every script uses it. Kenya, Uganda and Tanzania all use East Africa Time (EAT, UTC+3).

Formatting dates with date()

date($format) returns the current date and time as text. The format string uses single letters as placeholders.

<?php
echo "&copy; " . date("Y") . " Skope Digital Academy";
// &copy; 2026 Skope Digital Academy  (the current year)

Because "now" keeps changing, the remaining examples use a fixed moment so you get the same output as shown. mktime(hour, minute, second, month, day, year) builds a Unix timestamp: the number of seconds since 1 January 1970 (UTC). Pass the timestamp as the second argument to date().

Example PHP
<?php
date_default_timezone_set("Africa/Nairobi");
$ts = mktime(14, 30, 0, 3, 15, 2026);   // 15 March 2026, 2:30 pm

echo $ts . "\n";
echo date("Y-m-d", $ts) . "\n";
echo date("d/m/Y", $ts) . "\n";
echo date("l, j F Y", $ts) . "\n";
echo date("D j M, g:i a", $ts) . "\n";
echo date("H:i:s", $ts) . "\n";
Result
1773574200
2026-03-15
15/03/2026
Sunday, 15 March 2026
Sun 15 Mar, 2:30 pm
14:30:00
Open in editor

The most useful format characters:

CharacterMeaningExample
dday of the month, 2 digits05
jday of the month, no leading zero5
D / lshort / full weekday nameSun / Sunday
m / nmonth number, with / without zero03 / 3
M / Fshort / full month nameMar / March
Y / y4-digit / 2-digit year2026 / 26
H / Ghour 00-23 / 0-2314
h / ghour 01-12 / 1-1202 / 2
iminutes30
sseconds00
a / Aam/pm / AM/PMpm
Nday of week, 1 (Mon) to 7 (Sun)7
tnumber of days in the month31
Tip: Any character that is not a format letter is printed as-is, like the commas and slashes above. To print a letter literally, escape it with a backslash: date("jS \of F", $ts) gives 15th of March.

Converting text to dates with strtotime()

strtotime() turns a date written in English into a timestamp. It understands exact dates and relative phrases. The optional second argument is the starting point.

Example PHP
<?php
date_default_timezone_set("Africa/Nairobi");
$base = strtotime("2026-03-15 09:00");

echo date("Y-m-d", strtotime("+1 day", $base)) . "\n";
echo date("Y-m-d", strtotime("+2 weeks", $base)) . "\n";
echo date("Y-m-d", strtotime("next friday", $base)) . "\n";
echo date("Y-m-d", strtotime("last day of this month", $base)) . "\n";
var_dump(strtotime("not a date"));
Result
2026-03-16
2026-03-29
2026-03-20
2026-03-31
bool(false)
Open in editor

strtotime() returns false when it cannot understand the text, so check the result before using it, especially with user input.

The DateTime classes

For anything more than simple formatting, use the date objects. DateTimeImmutable is the safest choice: methods like modify() return a new object instead of changing the original, which prevents subtle bugs.

Example PHP
<?php
$tz = new DateTimeZone("Africa/Nairobi");
$start = new DateTimeImmutable("2026-01-05 08:00", $tz);
$end = $start->modify("+3 months");

echo "Term starts: " . $start->format("D j M Y") . "\n";
echo "Term ends:   " . $end->format("D j M Y") . "\n";
echo "Timezone:    " . $start->format("T (P)") . "\n";
Result
Term starts: Mon 5 Jan 2026
Term ends:   Sun 5 Apr 2026
Timezone:    EAT (+03:00)
Open in editor

The format() method uses the same letters as date().

Differences between dates

diff() compares two dates and returns a DateInterval describing the gap. Its days property holds the total number of days.

Example PHP
<?php
$today = new DateTimeImmutable("2026-03-15");
$exam = new DateTimeImmutable("2026-06-01");

$gap = $today->diff($exam);
echo "Days to exam: " . $gap->days . "\n";
echo $gap->format("%m months and %d days") . "\n";

$born = new DateTimeImmutable("2007-08-20");
echo "Age: " . $born->diff($today)->y . " years";
Result
Days to exam: 78
2 months and 17 days
Age: 18 years
Open in editor

This age calculation correctly handles birthdays that have not happened yet this year, which is easy to get wrong with simple subtraction.

Comparing and validating dates

Date objects can be compared with the normal comparison operators. To check that a date typed into a form is real, parse it with createFromFormat() and confirm that formatting it back gives the same text.

Example PHP
<?php
function isValidDate(string $value): bool
{
    $d = DateTimeImmutable::createFromFormat("Y-m-d", $value);
    return $d !== false && $d->format("Y-m-d") === $value;
}

var_dump(isValidDate("2026-02-28"));
var_dump(isValidDate("2026-02-30"));
var_dump(isValidDate("28/02/2026"));

$deadline = new DateTimeImmutable("2026-03-31");
$paidOn = new DateTimeImmutable("2026-04-02");
echo $paidOn > $deadline ? "Late payment" : "On time";
Result
bool(true)
bool(false)
bool(false)
Late payment
Open in editor

The second check fails because there is no 30 February, even though createFromFormat() would quietly roll it over to 2 March.

Note: Store dates in your database as DATE or DATETIME columns in the standard Y-m-d H:i:s format. Only convert to friendly formats like "Sun 15 Mar" when displaying them.
Exercise

Print the current year as four digits.

echo date("");
Exercise

Set the default time zone for Kenya.

date_default_timezone_set("");
Exercise

Get the timestamp for one week after $base.

$next = ("+1 week", $base);
Sign in or create a free account to track progress, earn XP, badges and a certificate.