PHP date() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

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);
CallWhat 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.

Library declarations
PHP
date(string $format, ?int $timestamp = null): string

Parameters

ParameterDescription
formatRequired. 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.
timestampOptional. A Unix timestamp as an integer. If it is null or left out, the current time from time() is used.

Return Value

Return ValueDescription
stringThe 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.

PHP
<?php
date_default_timezone_set('UTC');
$ts = strtotime('2024-01-15 10:30:00');

echo date('Y-m-d', $ts), PHP_EOL;
echo date('H:i:s', $ts), PHP_EOL;
echo date('d/m/Y', $ts), PHP_EOL;
echo date('l, F j, Y', $ts), PHP_EOL;

How It Works

  1. date_default_timezone_set('UTC') makes the result the same on every computer.
  2. strtotime() converts the fixed date string into a Unix timestamp.
  3. Each date() call uses the same timestamp but a different format string.
  4. Letters like Y, m, d, H, i and s are replaced with the matching part of the date.

Example 2 — Epoch, 12 Hour Time and Escaped Text

Timestamp 0 is the Unix epoch. You can also use 12 hour time and escape letters with a backslash so they are printed as normal text.

PHP
<?php
date_default_timezone_set('UTC');
$ts = strtotime('2024-01-15 10:30:00');

echo date('Y-m-d H:i:s', 0), PHP_EOL;
echo date('g:i A', $ts), PHP_EOL;
echo date('jS \o\f F', $ts), PHP_EOL;
echo "Day of year: ", date('z', $ts), ", week: ", date('W', $ts), PHP_EOL;

How It Works

  1. Timestamp 0 is the Unix epoch, which is January 1, 1970 at midnight UTC.
  2. g:i A prints the hour without a leading zero, the minutes and AM or PM.
  3. The backslashes in \o\f stop PHP from treating o and f as format letters, so the words 'of' appear.
  4. The letter z counts days from zero, so January 15 is day 14, and W gives the ISO week number 03.

Example 3 — The Timezone Changes the Result

The same timestamp prints a different clock time when the default timezone changes. The letter T adds the timezone abbreviation.

PHP
<?php
$ts = strtotime('2024-01-15 10:30:00 UTC');

date_default_timezone_set('UTC');
echo date('H:i T', $ts), PHP_EOL;

date_default_timezone_set('Asia/Kolkata');
echo date('H:i T', $ts), PHP_EOL;

date_default_timezone_set('America/New_York');
echo date('H:i T', $ts), PHP_EOL;

How It Works

  1. The timestamp is created from a string that says UTC, so it always means the same moment.
  2. With the UTC timezone, date() prints 10:30.
  3. India is 5 hours 30 minutes ahead of UTC, so the same moment prints as 16:00 IST.
  4. 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.

Next: date_add()

date_add() adds a DateInterval, such as 10 days or 1 month, to a DateTime object. It chang.

date_add() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful