Blog
Wild & Free Tools

Convert Unix Timestamp in PHP — date(), strtotime, and DateTime Methods

Last updated: April 2026 6 min read

Table of Contents

  1. date() Function
  2. DateTime Class
  3. Timezones in PHP
  4. Milliseconds in PHP
  5. Frequently Asked Questions

PHP has more ways to handle Unix timestamps than any other language and they are all subtly different. date() is the simplest and oldest. DateTime is the modern object-oriented approach. strtotime() parses almost any human-written date string.

This guide covers all three, when to use each, and the timezone gotchas that have shipped real bugs. Code is PHP 7+ compatible.

The date() Function — Simplest Conversion

PHP's date() function converts a Unix timestamp to a formatted string. The first argument is a format string, the second is the timestamp.

<?php
$ts = 1711000000;

echo date('Y-m-d H:i:s', $ts);
// 2024-03-21 07:46:40

echo date('F j, Y g:i A', $ts);
// March 21, 2024 7:46 AM

echo date('c', $ts);
// 2024-03-21T07:46:40+00:00 (ISO 8601)

// Current time
echo date('Y-m-d H:i:s'); // omit second arg = now
?>

The format characters are documented in the PHP manual but the most common ones are: Y (4-digit year), m (month), d (day), H (24-hour hour), i (minute), s (second). The c format produces ISO 8601 directly.

Going the other way — date string to Unix timestamp

<?php
// Current Unix timestamp
$ts = time();

// Specific date string
$ts = strtotime('2024-03-21 07:46:40');
$ts = strtotime('March 21, 2024 7:46 AM');
$ts = strtotime('next monday');
$ts = strtotime('+1 week');
?>

strtotime is one of PHP's best features — it parses almost anything that looks like a date. The downside is it silently returns false on inputs it cannot parse, so always check: if ($ts === false) { ... }.

The DateTime Class — Modern Approach

For new code, use the DateTime object. It handles timezones explicitly and has a cleaner API for arithmetic.

<?php
// From Unix timestamp
$dt = (new DateTime())->setTimestamp(1711000000);
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s'); // 2024-03-21 03:46:40

// From a date string in a specific timezone
$dt = new DateTime('2024-03-21 12:00:00', new DateTimeZone('UTC'));
echo $dt->getTimestamp(); // 1711022400

// Date arithmetic
$dt->modify('+1 day');
$dt->add(new DateInterval('PT2H')); // 2 hours

// Difference between two dates
$dt1 = new DateTime('2024-01-01');
$dt2 = new DateTime('2024-12-31');
$diff = $dt1->diff($dt2);
echo $diff->days; // 365
?>

DateTime is mutable by default — methods like modify() change the original object. If you want immutable behavior, use DateTimeImmutable instead. The API is identical, but every method returns a new object instead of modifying.

Sell Custom Apparel — We Handle Printing & Free Shipping

PHP Timezone Handling

PHP defaults to whatever timezone is set in php.ini. This is the source of more production bugs than any other PHP feature. Always set it explicitly:

<?php
// Set globally for the script
date_default_timezone_set('UTC');

// Or pass it per-call
$dt = new DateTime('now', new DateTimeZone('UTC'));
?>

Best practice: set the default timezone to UTC in your application bootstrap, store all timestamps in UTC in the database, convert to user timezones only at display time. This is the same pattern that works in every language.

The DST gotcha

If you store timestamps as datetime strings without a timezone (which PHP loves to do), you lose information at every daylight savings transition. A datetime string like "2024-03-10 02:30:00" in America/New_York is ambiguous — that exact local time may not exist or may exist twice depending on the date. Unix timestamps have no DST ambiguity, which is why they are the right storage format.

Milliseconds and Microseconds

PHP time() returns seconds. For milliseconds, use microtime(true) * 1000 and floor it. For converting from a millisecond timestamp:

<?php
// Current time in milliseconds
$ms = (int)(microtime(true) * 1000);

// Convert millisecond timestamp to a DateTime
$msTimestamp = 1711000000123;
$dt = DateTime::createFromFormat('U.u', sprintf('%.3f', $msTimestamp / 1000));
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s.v');
?>

PHP's millisecond support is awkward compared to Python or JavaScript. If your codebase deals with milliseconds frequently (working with JavaScript APIs, for example), consider standardizing on storing them as strings or BIGINT in the database to avoid the casting headache.

For a quick one-off conversion the free Unix timestamp converter auto-detects seconds vs milliseconds.

Try It Free — No Signup Required

Runs 100% in your browser. No data is collected, stored, or sent anywhere.

Open Free Unix Timestamp Converter

Frequently Asked Questions

How do I convert a Unix timestamp to a date in PHP?

Use date("Y-m-d H:i:s", $timestamp) for a simple formatted string, or (new DateTime())->setTimestamp($timestamp) for the object-oriented approach. The first is shorter, the second handles timezones explicitly without modifying global state.

What is the difference between time() and microtime() in PHP?

time() returns the current Unix timestamp as an integer in seconds. microtime(true) returns it as a float with microsecond precision. Multiply microtime(true) by 1000 to get milliseconds, by 1000000 to get microseconds.

How do I get a Unix timestamp from a date string in PHP?

strtotime("2024-03-21 12:00:00") parses almost any date string and returns a Unix timestamp. It also handles relative formats like "next monday" or "+1 week". Always check for false return value because strtotime returns false on parse failure.

How do I set the timezone for date() in PHP?

Either set it globally with date_default_timezone_set("UTC") at the top of your script, or use DateTime with an explicit DateTimeZone. The global setting affects every date function call, which is convenient but also makes timezone bugs hard to track down.

Why does my PHP date() return the wrong time?

Almost always a timezone misconfiguration. PHP uses whatever timezone is set in php.ini, which on most servers defaults to UTC but on some defaults to the server's local time. Set it explicitly with date_default_timezone_set() at the top of every script.

Should I use DateTime or DateTimeImmutable in PHP?

Use DateTimeImmutable for new code. The mutable DateTime class causes bugs because methods like modify() change the original object — if you pass a DateTime to a function and it modifies the date, you have a bug nobody will catch until production. Immutable returns a new object each time.

Launch Your Own Clothing Brand — No Inventory, No Risk