Date & Time
Learn how to read, format, parse, and calculate dates and times in PHP using date(), time(), DateTime, and strtotime().
Introduction
Almost every real application needs to work with dates and times — a blog post's publish date, a session's expiry time, an invoice due date, or a "posted 3 hours ago" label. PHP has been handling this since its earliest versions, and it offers two complementary approaches: a set of simple built-in functions, and a more powerful object-oriented DateTime class.
In this lesson, you will learn both styles: the classic date() and time() functions for quick formatting, and the modern DateTime class for anything more involved, along with strtotime() for turning human-readable text into a usable date.
- How to get the current Unix timestamp with time().
- How to format dates and times with date() and format characters.
- How the object-oriented DateTime class works.
- How to parse human-readable strings with strtotime().
- How to perform date arithmetic, such as adding days to a date.
Unix Timestamps with time()
Internally, PHP (like most programming languages) represents a moment in time as a Unix timestamp: the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC. The time() function returns this value for the current moment.
<?php
$now = time();
echo $now;
// Example output: 1785000000 (an integer, seconds since 1970)1785000000A raw timestamp is not very readable on its own, but it is extremely useful for storing dates in a database, comparing two moments in time, or measuring how much time has passed between events.
Because a timestamp is just an integer, you can compare two dates with simple math (e.g. $end - $start) or store them compactly in a database column, without worrying about time zones or string formats.
Formatting Dates with date()
The date() function takes a format string and turns a timestamp into a human-readable string. If you do not pass a timestamp, it uses the current time by default.
<?php
echo date("Y-m-d");
echo "\n";
echo date("H:i:s");
echo "\n";
echo date("Y-m-d H:i:s");2026-07-26
14:30:00
2026-07-26 14:30:00You can also format a specific timestamp, rather than the current time, by passing it as the second argument.
<?php
$timestamp = time();
echo date("l, F j, Y", $timestamp);
// Example: Sunday, July 26, 2026Sunday, July 26, 2026Common Format Characters
The date() function reads its format string one character at a time, replacing recognized letters with pieces of the date. Here are the ones you will use most often.
| Character | Meaning | Example |
|---|---|---|
| Y | Four-digit year | 2026 |
| y | Two-digit year | 26 |
| m | Month, with leading zero | 07 |
| n | Month, no leading zero | 7 |
| d | Day of month, with leading zero | 26 |
| j | Day of month, no leading zero | 26 |
| H | 24-hour format hour | 14 |
| i | Minutes, with leading zero | 30 |
| s | Seconds, with leading zero | 00 |
| l | Full day name | Sunday |
| F | Full month name | July |
| A | Uppercase AM/PM | PM |
Any character in the format string that is not a recognized letter is printed as-is, so date("Y-m-d") produces dashes literally. To include a literal letter that PHP would otherwise interpret (like the letter "T"), escape it with a backslash.
The DateTime Class
The date() and time() functions are quick and convenient, but for anything more involved — comparing dates, calculating differences, or doing arithmetic — the object-oriented DateTime class is the more modern and flexible approach.
<?php
$now = new DateTime();
echo $now->format("Y-m-d H:i:s");
echo "\n";
$specificDate = new DateTime("2026-12-25");
echo $specificDate->format("l, F j, Y");2026-07-26 14:30:00
Friday, December 25, 2026The DateTime constructor accepts almost any human-readable date string, and its format() method uses the exact same format characters you just learned for date(). This makes DateTime feel familiar while opening the door to more advanced operations, like comparing two dates directly.
<?php
$date1 = new DateTime("2026-01-01");
$date2 = new DateTime("2026-12-25");
if ($date2 > $date1) {
echo "date2 is later than date1";
}date2 is later than date1- date() and time() are simple functions, great for quick formatting of the current moment.
- DateTime is a full object that can be compared, modified, and passed around your code.
- Both use the same format characters, so learning one prepares you for the other.
Parsing Strings with strtotime()
Often you will have a date as plain text — from a form, a file, or an API — and need to convert it into something PHP can work with. The strtotime() function parses a wide variety of human-readable date and time formats into a Unix timestamp.
<?php
$timestamp = strtotime("2026-12-25");
echo date("Y-m-d", $timestamp);
echo "\n";
$timestamp2 = strtotime("next Monday");
echo date("Y-m-d", $timestamp2);
echo "\n";
$timestamp3 = strtotime("+1 week");
echo date("Y-m-d", $timestamp3);2026-12-25
2026-07-27
2026-08-02strtotime() understands relative phrases like "tomorrow", "next Monday", "+1 week", and "-3 days", in addition to standard date formats. If it cannot understand the input, it returns false, so it is a good idea to check the result before using it.
Date Arithmetic
A common task is calculating a date relative to another one, such as "7 days from now" or "one month before this invoice date." PHP gives you two convenient ways to do this: passing a relative string to strtotime(), or using the DateInterval class with DateTime.
<?php
$tomorrow = strtotime("+1 day");
echo date("Y-m-d", $tomorrow);2026-07-27The DateTime approach uses the modify() method or a DateInterval object, which is often clearer when working with an object you already have.
<?php
$date = new DateTime("2026-07-26");
$date->add(new DateInterval("P7D")); // P7D means "a period of 7 days"
echo $date->format("Y-m-d");
echo "\n";
$date->sub(new DateInterval("P2D")); // subtract 2 days
echo $date->format("Y-m-d");2026-08-02
2026-07-31DateInterval specifications start with "P" (period) followed by a number and a unit: D for days, M for months, Y for years. A time component (hours, minutes, seconds) follows a "T", e.g. "PT1H30M" means 1 hour 30 minutes.
Common Mistakes
- Forgetting to set a default time zone, which can cause dates to be off by hours depending on the server configuration — use date_default_timezone_set() at the top of your script.
- Confusing "m" (numeric month) with "M" (short month name) in format strings.
- Assuming strtotime() always succeeds — it returns false for input it cannot parse.
- Mixing up add() and sub() on a DateTime object and forgetting that they modify the object in place rather than returning a new one.
Best Practices
- Set an explicit time zone with date_default_timezone_set("UTC") (or your desired zone) instead of relying on server defaults.
- Prefer the DateTime class for anything beyond simple formatting, especially date comparisons and arithmetic.
- Validate the result of strtotime() before using it, since invalid input returns false.
- Store dates in your database as standard formats (like Y-m-d H:i:s) so they sort and compare correctly.
Frequently Asked Questions
What is a Unix timestamp?
It is the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC. PHP's time() function returns the current timestamp, and many date functions accept or return one.
Should I use date()/time() or the DateTime class?
For quick, one-off formatting of the current time, date() is simple and fine. For comparing dates, doing arithmetic, or passing a date around your application, DateTime is the more robust, modern choice.
Why did my date come out a few hours off?
This is almost always a time zone issue. Set your desired time zone explicitly with date_default_timezone_set() at the start of your script.
Can strtotime() parse any date format?
It handles most common formats and relative phrases like "next Friday" or "+2 weeks", but it is not universal. If it cannot understand the string, it returns false.
How do I find the difference between two dates?
Create two DateTime objects and call diff() on one, passing the other as an argument. This returns a DateInterval object describing the difference in days, months, and years.
Key Takeaways
- time() returns the current Unix timestamp — seconds since January 1, 1970.
- date() formats a timestamp (or the current time) into a readable string using format characters like Y, m, d, H, i, and s.
- The DateTime class is the modern, object-oriented way to work with dates, and supports comparison and arithmetic.
- strtotime() converts human-readable date strings, including relative phrases, into a timestamp.
- You can add or subtract time using strtotime("+1 day") or a DateTime object with add()/sub() and DateInterval.
Summary
Working with dates and times is a routine part of almost every PHP application. The simple date() and time() functions cover quick formatting needs, while the DateTime class gives you a more powerful, object-oriented toolkit for comparisons and arithmetic, and strtotime() bridges the gap between human-readable text and usable timestamps.
Next, you will look at how PHP handles errors at a fundamental level — the traditional error system that existed long before exceptions became the preferred approach.
- You can get the current timestamp with time() and format it with date().
- You understand common date() format characters.
- You can create and manipulate dates with the DateTime class.
- You can parse human-readable strings with strtotime() and perform date arithmetic.