time()
Learn PHP date and time functions: time(), date(), strtotime(), DateTimeImmutable, DateInterval and DateTimeZone with runnable examples and a format reference.
Introduction
PHP provides a robust set of date and time functions that are essential for handling temporal data: logging events, scheduling, calculating durations, and displaying localized dates. This guide covers the most commonly used tools — time(), date(), strtotime(), and the modern DateTimeImmutable / DateInterval / DateTimeZone object API — with their syntax and practical, runnable examples.
A few concepts to know first:
- A Unix timestamp is an integer counting the seconds since January 1, 1970 00:00:00 UTC (the "epoch"). It is timezone-agnostic, which makes it ideal for storing and comparing moments in time.
- A format string turns a timestamp into a human-readable string. Each character (
Y,m,d,H, …) maps to a date component. - PHP renders dates using the server's default timezone. Set it explicitly to avoid surprises:
<?php
date_default_timezone_set('UTC');Setting the timezone up front makes date() and time() deterministic across servers. For a deeper introduction, see PHP Date and Time.
PHP Date Function
The date() function formats a timestamp into a readable string. It accepts two parameters: format (required) and timestamp (optional, defaults to current time). The format string uses specific characters to represent date components.
Example of date() function in PHP
In this example, Y, m, and d represent the four-digit year, two-digit month, and two-digit day, respectively. The format string supports many other characters for hours, minutes, seconds, and more.
Common format characters
| Char | Meaning | Example |
|---|---|---|
Y | 4-digit year | 2023 |
y | 2-digit year | 23 |
m | Month, zero-padded | 03 |
n | Month, no padding | 3 |
d | Day of month, zero-padded | 03 |
j | Day of month, no padding | 3 |
H | Hour, 24h, zero-padded | 14 |
g | Hour, 12h, no padding | 2 |
i | Minutes | 09 |
s | Seconds | 05 |
A | AM/PM | PM |
l | Day name (full) | Friday |
D | Day name (short) | Fri |
F | Month name (full) | March |
T | Timezone abbreviation | UTC |
Pass a second argument to format any timestamp, not just "now":
<?php
date_default_timezone_set('UTC');
echo date("l, F j, Y", 1678838400); // outputs "Wednesday, March 15, 2023"To escape a literal letter so it is not treated as a format character, prefix it with a backslash (e.g. date('jS \o\f F')). For more formatting options see date() and date_format().
PHP Time Function
The time() function returns the current Unix timestamp, which counts the number of seconds since January 1, 1970, 00:00:00 UTC.
How to use PHP time() function?
The returned integer can be passed directly to date() or other temporal functions for further processing. Because the timestamp is just a number, you can do arithmetic on it — for example, add 86400 (the number of seconds in a day) to get "this time tomorrow":
<?php
date_default_timezone_set('UTC');
$tomorrow = time() + 86400; // 24 * 60 * 60 seconds
echo date("Y-m-d H:i:s", $tomorrow);For date math that respects months, leap years, and daylight-saving time, prefer the object API shown below over raw second arithmetic.
PHP strtotime Function
The strtotime() function parses an English textual datetime description into a Unix timestamp. It accepts a single string argument.
PHP strtotime Function example
This converts the specified date and time string into its corresponding Unix timestamp value. strtotime() also understands relative expressions, which is handy for scheduling:
<?php
date_default_timezone_set('UTC');
echo date("Y-m-d", strtotime("next monday")), "\n";
echo date("Y-m-d", strtotime("2023-03-03 +1 week")); // outputs "2023-03-10"If the string cannot be parsed, strtotime() returns false, so always check the result before passing it on.
PHP DateInterval Class
The DateInterval class represents a duration between two dates or times. Modern PHP development typically uses DateTimeImmutable to calculate intervals, as it avoids mutating the original date object.
The PHP DateInterval class example
This calculates the difference between two dates. The %R%a format specifier outputs the sign (+ or -) followed by the total number of days.
You can also construct a DateInterval directly from an ISO 8601 duration string and add or subtract it from a date. P1W means "a period of one week":
<?php
$start = new DateTimeImmutable('2023-03-03');
$later = $start->add(new DateInterval('P1W'));
echo $later->format('Y-m-d'); // outputs "2023-03-10"Because DateTimeImmutable never mutates the original, $start still points at March 3rd after the add() call. See date_add(), date_sub(), and date_diff() for the procedural equivalents.
PHP Timezone Functions
PHP handles time zones through the DateTimeZone class and related methods. These allow conversion between zones and display of UTC offsets.
Example of PHP timezone functions
This outputs the date and time in the specified zone, along with its abbreviation (e.g., EST).
To convert an existing moment from one zone to another, use setTimezone(). The instant in time stays the same — only its representation changes:
<?php
$ny = new DateTimeImmutable('2023-03-03 12:00:00', new DateTimeZone('America/New_York'));
$utc = $ny->setTimezone(new DateTimeZone('UTC'));
echo $utc->format('Y-m-d H:i:s T'); // outputs "2023-03-03 17:00:00 UTC"New York is five hours behind UTC in March, so noon in New York is 17:00 UTC. To change the timezone used by the procedural date() and time() functions, call date_default_timezone_set().
Procedural vs. object API: which to use
PHP offers two styles for working with dates:
- Procedural functions (
time(),date(),strtotime(), getdate(), gmdate()) are concise and great for quick formatting or one-off conversions. - The object API (
DateTimeImmutable,DateInterval,DateTimeZone) is the recommended choice for anything involving timezones, durations, or date math, because it is explicit, immutable, and avoids the pitfalls of integer-second arithmetic across DST boundaries.
A good rule of thumb: reach for DateTimeImmutable whenever a date crosses a timezone or needs to be added to or subtracted from.
Conclusion
Mastering PHP's date and time functions enables developers to handle temporal data accurately across different environments and time zones. Start with time() and date() for simple cases, lean on strtotime() to parse human input, and use the DateTimeImmutable family for reliable date math.