PHP has a reputation for inconsistency, but its date/time functions are genuinely powerful — once you get past the parameter order. If you've ever written date() and then spent five minutes looking up whether m means month or minute, you're not alone. But once you learn the patterns, PHP makes timestamp handling surprisingly ergonomic.

Let's walk through what it's actually like to work with Unix timestamps in PHP, from the basics to the edge cases that'll bite you at 3 AM.

The Basics: Getting Timestamps

Getting the current Unix timestamp is the simplest thing in PHP:

$now = time();
echo $now; // 1719820800 (10 digits, seconds)

That's it. time() returns the current Unix timestamp in seconds. No parameters, no confusion. For milliseconds (common in JavaScript interop), you'll need a bit of math:

$millis = intval(microtime(true) * 1000);

microtime(true) returns the current time as a float with microsecond precision. Multiply by 1000 and cast to int, and you've got your 13-digit timestamp.

Formatting a Timestamp with date()

The date() function is where PHP's date format characters shine — and confuse. The function signature is:

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

The timestamp defaults to time() if omitted, so date('Y-m-d') gives you today's date.

echo date('Y-m-d');            // 2026-07-11
echo date('Y-m-d H:i:s');     // 2026-07-11 14:30:00
echo date('Y-m-d H:i:s T');   // 2026-07-11 14:30:00 CST

The most common format characters to remember:

Char Meaning Example
Y 4-digit year 2026
m 2-digit month 07
d 2-digit day 11
H 24-hour hour 14
i Minutes 30
s Seconds 00
T Timezone abbreviation CST
U Unix timestamp 1719820800

Pro tip: If you need to format a specific timestamp, pass it as the second argument:

echo date('Y-m-d', 1719820800); // 2024-07-01

Parsing Strings with strtotime()

This is where PHP really stands out. strtotime() parses human-readable date strings into Unix timestamps:

echo strtotime('2024-07-01');          // depends on timezone*
echo strtotime('next Monday');         // varies
echo strtotime('+1 day');              // tomorrow
echo strtotime('last day of this month'); // end of month
echo strtotime('2024-07-01 14:30:00'); // with time

* strtotime('2024-07-01') uses the server's default timezone. In UTC+8 it gives 1719820800; in UTC it gives 1719792000. Same date, different numbers — that's exactly the gotcha we'll cover in the timezone section below.

The magic is in the relative formats. You can chain them:

echo strtotime('+1 week 2 days 4 hours');
echo strtotime('first day of next month');
echo strtotime('last Sunday');

This is incredibly useful for generating date ranges, cron schedules, or subscription expiration dates without manual date arithmetic.

Real-World Scenario: Processing an API Response

Let's say you're consuming a REST API that returns an ISO 8601 date string, and you need to display it in a specific format:

$apiResponse = '2024-07-01T00:00:00Z';

$timestamp = strtotime($apiResponse);
if ($timestamp === false) {
    die('Invalid date string');
}

echo date('F j, Y \a\t g:i A', $timestamp);
// July 1, 2024 at 12:00 AM

strtotime() handles ISO 8601, RFC 2822, MySQL format, and dozens of other formats out of the box. When it can't parse something, it returns false — always check for that.

Going the Other Way: Timestamp to String (with Format)

If you have a Unix timestamp from a database and need to format it:

$dbTimestamp = 1719820800; // from MySQL INT column

echo date('Y-m-d H:i:s', $dbTimestamp);    // 2024-07-01 08:00:00
echo date('D, M j, Y', $dbTimestamp);      // Mon, Jul 1, 2024
echo date('Y/m/d', $dbTimestamp);          // 2024/07/01

Notice the time shows 08:00:00 — that's because date() uses the server's default timezone. We'll get to that.

Timezones: The Silent Bug Factory

By default, date() and strtotime() use the timezone set in php.ini or via date_default_timezone_set(). If you've ever deployed a PHP app to a server in a different region and seen dates shift by a day, this is why.

// Set explicitly — don't rely on php.ini
date_default_timezone_set('UTC');

echo date('Y-m-d H:i:s', 1719820800); // 2024-07-01 00:00:00

For user-facing applications, you often need to convert to the user's timezone. The DateTime and DateTimeZone classes give you proper control:

$timestamp = 1719820800;
$userTz = new DateTimeZone('Asia/Tokyo');

$dt = new DateTime("@$timestamp"); // UTC
$dt->setTimezone($userTz);

echo $dt->format('Y-m-d H:i:s'); // 2024-07-01 09:00:00

Note the @ prefix — it tells DateTime to treat the string as a Unix timestamp. This is a handy trick when you need timezone-aware formatting.

Common Pitfalls

1. strtotime() Returns False (Not 0) on Failure

$ts = strtotime('not a date');
if ($ts === false) {
    // Handle error — don't pass false to date()!
}

Passing false to date() will be interpreted as a timestamp at the Unix epoch (January 1, 1970), not an error.

2. m/d/y vs d/m/y Confusion

echo strtotime('03/04/2024'); // March 4 or April 3?

PHP interprets 03/04/2024 as March 4 (MM/DD/YYYY) because slash-separated strings are parsed as US format. Use dashes (03-04-2024) for European (DD-MM-YYYY) parsing, or better, use ISO format (2024-03-04).

3. Relying on the Server Timezone

// Server in UTC, but users in Asia
echo date('Y-m-d', $timestamp); // Might be yesterday or today!

Always set your timezone explicitly, or use DateTime with a specific timezone.

4. strtotime() Ambiguity: "+1 Month"

echo date('Y-m-d', strtotime('+1 month', strtotime('2024-01-31')));
// 2024-03-02 — not 2024-02-29!

PHP's +1 month simply adds 30-something days in a naive way. For precise month arithmetic, use DateTime::modify().

The DateTime Alternative

While strtotime() and date() are convenient, PHP 5.2+ introduced the DateTime family for a reason. For complex operations, reach for these instead:

$dt = new DateTime('2024-07-01');
$dt->modify('+3 months');
$dt->modify('first day of this month');
echo $dt->format('Y-m-d');

// Diff between two dates
$now = new DateTime();
$interval = $now->diff($dt);
echo $interval->days; // total days

DateTime is more predictable, especially with timezone handling and month arithmetic.

Quick Reference: strtotime() English Formats

Format Result
now Current timestamp
tomorrow Midnight tomorrow
yesterday Midnight yesterday
+1 day Same time, next day
next week Same time, next week
last Monday Previous Monday at midnight
first day of January 2025 2025-01-01 midnight
next month Same day next month (be careful!)

Wrapping Up

PHP's strtotime() and date() are deceptively simple tools that handle most everyday timestamp needs. The key takeaways:

  • Use time() for the current timestamp, date() for formatting, strtotime() for parsing
  • Always check that strtotime() returns false on failure
  • Set the timezone explicitly with date_default_timezone_set() or use DateTime
  • Watch out for m/d/y parsing quirks and +1 month surprises
  • For anything complex, skip the functions and use DateTime directly

The next time you need to calculate "next Wednesday" or format a timestamp for a user in Tokyo, PHP has you covered — just remember to check the timezone first.

For quick conversions without touching your editor, the FastUnix Timestamp Converter is always a handy bookmark.