Unix Timestamp in PHP
PHP uses whole seconds natively via time(). Since PHP 8, 64-bit builds handle dates well past 2038 without trouble.
Need to convert one value rather than write code? Use the interactive converter — it handles every unit and time zone without leaving your browser.
Current Unix timestamp
<?php
echo time(); // 1710050400 (seconds)
echo (int) (microtime(true) * 1000); // milliseconds
echo (new DateTimeImmutable())->getTimestamp(); Timestamp to date
<?php
$ts = 1710050400;
echo date('Y-m-d H:i:s', $ts); // server's default zone
$d = (new DateTimeImmutable("@$ts")) // "@" means "this is an epoch"
->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $d->format('Y-m-d H:i:s T'); // 2024-03-10 11:30:00 IST
echo $d->format(DateTimeInterface::ATOM); // ISO 8601 Date to timestamp
<?php
$d = new DateTimeImmutable('2024-03-10 11:30:00', new DateTimeZone('Asia/Kolkata'));
echo $d->getTimestamp(); // 1710050400
echo strtotime('2024-03-10T06:00:00Z'); // quick but locale-sensitive
echo mktime(11, 30, 0, 3, 10, 2024); // uses date_default_timezone_get() Setting the default zone
<?php
// Do this once at bootstrap, or set date.timezone in php.ini.
date_default_timezone_set('UTC');
// Storing in MySQL: keep the column UTC and convert on display.
$utc = (new DateTimeImmutable('now', new DateTimeZone('UTC')))
->format('Y-m-d H:i:s'); Pitfalls specific to PHP
- A
DateTimebuilt from"@1710050400"always carries UTC — callingsetTimezone()changes the display, not the instant, which is correct but surprises people. strtotime()silently returnsfalseon input it cannot parse, andfalsecompares equal to0, i.e. 1970. Always check the return value explicitly.- Prefer
DateTimeImmutableoverDateTime; the mutable version modifies in place and causes action-at-a-distance bugs.
Rules that apply in every language
- Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
- Name the unit in the identifier.
expires_at_msrather thanexpires_atcosts nothing and prevents the single most common timestamp bug. - Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
- Use 64-bit time. Anything still storing seconds in a signed 32-bit field breaks in January 2038 — see the Year 2038 problem.
The same task in other languages
- Python float seconds
- JavaScript integer milliseconds
- Java integer milliseconds
- Go integer seconds and nanoseconds
- TypeScript integer milliseconds
- Ruby float seconds
- C# ticks (100 ns)
- Rust seconds + nanoseconds
- SQL varies by engine
- Bash integer seconds
- C++ chrono duration
- Swift float seconds
- Kotlin integer milliseconds
- C time_t seconds
- Perl integer seconds
- Dart integer milliseconds
- PowerShell .NET DateTimeOffset
- Scala integer milliseconds
- R float seconds
- Excel days since 1899-12-30
Frequently asked questions
How do I get the current Unix timestamp in PHP?
Use the snippet in the "Current Unix timestamp" section above. PHP works in integer seconds natively, so converting to another unit is a multiplication or an integer division away.
How do I convert a Unix timestamp to a date in PHP?
The "Timestamp to date" snippet above shows the idiomatic approach, including how to render the result in a specific time zone rather than whatever zone the machine happens to be set to.
Does PHP handle time zones and daylight saving correctly?
Yes, provided you pass an explicit zone rather than relying on the system default. The gotchas listed on this page cover the specific ways PHP makes that easy to get wrong.