Master the PHP date() function with this comprehensive guide. Learn how to format timestamps and create custom date strings with practical examples and detailed explanations.
You’ve got a Unix timestamp, or you just need “today” on the page, and you want it to read like something a person would write. That’s the job PHP’s date() function does. Give it a format string, get back a formatted date. It’s one of the first tools most of us reach for, and it’s still a good one.
Let’s walk through how it works, which format characters you’ll actually use, and one pitfall that trips people up more than it should.
The date() Function: An Overview
date() takes a format string and turns a timestamp into a readable date or time. The syntax is short:
<?php
date(format, timestamp);- The format parameter is the string that describes how you want the date to look.
- The timestamp parameter is optional. Leave it off and PHP uses the current date and time.
So most of the time you’ll pass a format and nothing else, and you get “now” formatted the way you asked. Here’s that in practice:
<?php
echo date("Y-m-d H:i:s"); // Output: 2023-02-28 08:30:15The format string "Y-m-d H:i:s" reads as year-month-day, then hour:minute:second. Every letter maps to one piece of the date.
Understanding Format Characters
Each character in the format string stands for a component of the date or time. There are a lot of them, but you’ll lean on the same handful most days. Here are the ones worth memorizing:
Y– Year (4 digits)y– Year (2 digits)m– Month (leading zeros)n– Month (no leading zeros)M– Month (short name)F– Month (full name)d– Day of the month (leading zeros)j– Day of the month (no leading zeros)D– Day of the week (short name)l– Day of the week (full name)h– Hour (12-hour format, leading zeros)H– Hour (24-hour format, leading zeros)i– Minutes (leading zeros)s– Seconds (leading zeros)a– am/pm (lowercase)A– AM/PM (uppercase)T– Timezone abbreviationO– Timezone offset from GMT (like +0200)
One thing to know: any letter with a meaning gets replaced. If you want a literal letter that happens to be a format character, escape it with a backslash. Mix and match the rest and you can build almost any layout you need.
Practical Examples
A few common formats, so you can see the characters doing real work.
Example 1: Year, Month, Day (with leading zeros)
<?php
echo date("Y-m-d"); // Output: 2023-02-28The classic "Y-m-d". Sorts cleanly, plays nice with databases, hard to misread.
Example 2: Day, Month, Year (no leading zeros)
<?php
echo date("j-n-y"); // Output: 28-2-23Same date, no padding on the day or month, and a two-digit year.
Example 3: Month/Day/Year Format
<?php
echo date("m/d/Y"); // Output: 02/28/2023The format most people in the United States expect to see.
Example 4: ISO 8601 Date with Timezone Offset
<?php
echo date("c"); // Output: 2023-02-28T08:30:15-05:00The "c" character is a shortcut for full ISO 8601, offset included. Reach for this when a date crosses a boundary, an API, a feed, a log, where the timezone has to travel with it.
Example 5: Full Day of the Week, Month, Day, Year
<?php
echo date("l, F j, Y"); // Output: Tuesday, February 28, 2023"l, F j, Y" spells everything out. This is the version you show a human.
One thing that will bite you: timezones
date() formats against PHP’s default timezone, not the reader’s and not your server’s clock in isolation. If you never set that default, PHP falls back to UTC, and the same code can print a time that’s hours off from what people expect. It’s the most common date bug there is, and it’s silent.
Set it explicitly, once, near the start of your app:
<?php
date_default_timezone_set("America/New_York");Better still, set date.timezone in your php.ini so every script agrees. Don’t leave it to chance.
When to reach past date()
date() is great for formatting the current moment. Once you’re doing date math, adding a week, comparing two dates, working across zones, the object API is easier to keep correct. That’s DateTimeImmutable, DateTime, plus DateTimeZone for zones and DateInterval for durations.
Between the two object classes, prefer DateTimeImmutable. Its methods return a new object instead of changing the one you’re holding, so an operation buried three functions deep can’t quietly rewrite a date you’re still using elsewhere. DateTime is mutable and that’s a real source of hard-to-find bugs. Same idea, safer default.
For displaying a date, though, date() still earns its place. Learn the format characters, set your timezone, and it’ll handle most of what you throw at it.


