In PHP, date() turns a Unix timestamp into a readable date and time string. If you do not pass a timestamp, it uses the current time. The format argument chooses letters such as Y-m-d.
The format string is made of letters that each stand for a part of the date, such as Y for the four digit year, m for the month and d for the day. The result depends on the default timezone, so set it with date_default_timezone_set() if you want the same output on every server.
Syntax
Call date() with the values you need. Start with this line.
PHP
echo date('Y-m-d H:i:s', $timestamp);
Call
What it does
date('Y-m-d', $ts)
Returns a date such as 2024-01-15.
date('H:i', $ts)
Returns the time in 24 hour format, such as 10:30.
date('l, F j, Y', $ts)
Returns a long date such as Monday, January 15, 2024.
date() works with Unix timestamps (seconds since January 1, 1970 UTC). Use strtotime() or mktime() to create a timestamp. To print a specific letter literally, put a backslash before it, like \T. For modern code that needs timezone control, DateTimeImmutable::format() is often a better choice.
Required. The format string. Common letters are Y (year), m (month), d (day), H (24 hour), i (minutes), s (seconds), D or l (weekday name), M or F (month name) and A (AM or PM). Unknown characters are printed as they are.
timestamp
Optional. A Unix timestamp as an integer. If it is null or left out, the current time from time() is used.
Return Value
Return Value
Description
string
The formatted date and time. The result uses the default timezone, which you can read with date_default_timezone_get().
PHP Examples
Example 1 — Formatting a Fixed Timestamp
Use strtotime() to build a timestamp from a fixed date, then format it in several ways. The timezone is set to UTC so the output never changes.
The timestamp is created from a string that says UTC, so it always means the same moment.
With the UTC timezone, date() prints 10:30.
India is 5 hours 30 minutes ahead of UTC, so the same moment prints as 16:00 IST.
New York is 5 hours behind UTC in January, so it prints 05:30 EST.
Frequently Asked Questions
It formats a Unix timestamp as a date and time string. You describe the layout with a format string such as 'Y-m-d H:i:s'. Without a timestamp, it formats the current time.
Y is the four digit year, m is the two digit month, d is the two digit day, H is the 24 hour clock, i is minutes, s is seconds, D is the short weekday name, l is the full weekday name, M is the short month name and F is the full month name.
date() uses the default timezone. If it is not what you expect, call date_default_timezone_set() with a timezone such as 'Asia/Kolkata' or set date.timezone in php.ini. You can check the current value with date_default_timezone_get().
Put a backslash before the letter. In a single quoted PHP string, '\T\o\d\a\y' prints the word Today. Remember that in a double quoted string you would need to double the backslashes.
date() works with integer timestamps and the default timezone. DateTime::format() and date_format() work with objects that carry their own timezone and support a wider range of dates, so they are better for complex date work.