Quick Answers: PHP Timestamp
- time() gives Unix seconds. It is the 10 digit timestamp, ready to store or send.
- date() formats a timestamp, but it uses the server default time zone, so set UTC first.
- DateTime with the at sign is the careful way. new DateTime('@1715429912') always starts in UTC.
- strtotime() is quick, DateTime is careful. Use DateTime when the input zone matters.
PHP Counts Seconds, Like Unix Does
PHP counts time the same way Unix does: in seconds since the epoch. time() returns that count as an integer, and microtime(true) returns the same instant as a float with microseconds included. That makes PHP straightforward for timestamp work, because the value coming out of the standard library is already the value most databases and APIs expect. The trap in PHP is not the unit but the time zone: date() and strtotime() use the server default zone, so the same code gives a different timestamp on two servers with different settings.
The converter above shows the same numbers live, and it detects all four precisions from the digit count, so a millisecond value pasted in from JavaScript is recognised at once instead of being read as a date far in the future.
Current Unix Timestamp in PHP
<?php
// Current Unix timestamp in seconds
$now = time();
// Current timestamp with microseconds
$nowFloat = microtime(true);time() is the standard way to read the clock in PHP. It returns whole seconds, which is the unix timestamp, and it is the value to store in a database column or send in an API call. microtime(true) is for measuring short intervals, where the fractional seconds matter. For logging, the seconds value is the one to keep: it is 10 digits, it sorts as a number, and every other language on this site reads it the same way.
Unix Timestamp to Date in PHP
<?php
date_default_timezone_set('UTC');
// Unix seconds to date
echo date('Y-m-d H:i:s', 1715429912);
// 2024-05-11 12:18:32
// With DateTime, always UTC with the @ sign
$dt = new DateTime('@1715429912');
echo $dt->format('Y-m-d H:i:s'); // 2024-05-11 12:18:32The reference value used across this site is 1715429912, which is Saturday, 11 May 2024 at 12:18:32 UTC. date('Y-m-d H:i:s', 1715429912) prints exactly that, once the default zone is set to UTC. The DateTime way is more explicit: new DateTime('@1715429912') builds the date in UTC no matter what the server default is, because the at sign tells the constructor the value is a timestamp. For code that runs on servers you do not control, the DateTime way is the safer one.
Date to Unix Timestamp in PHP
<?php
// Date to Unix timestamp, quick way
$ts = strtotime('2024-05-11 12:18:32 UTC');
echo $ts; // 1715429912
// Date to Unix timestamp, careful way
$dt = new DateTime(
'2024-05-11 12:18:32',
new DateTimeZone('UTC')
);
echo $dt->getTimestamp(); // 1715429912strtotime() is the quick way: strtotime('2024-05-11 12:18:32 UTC') gives 1715429912 in one call. The DateTime way is longer but clearer about the zone, because the DateTimeZone is passed in explicitly instead of depending on the server default. When the input comes from a user, a form, or another system, use the DateTime way. When the input is a fixed string you wrote yourself, strtotime() is fine. The converter at the top of this page accepts the same date strings, so you can check a parsing result there before trusting it in code.
PHP Timestamps in Other Time Zones
<?php
// One instant in another time zone
$dt = new DateTime('@1715429912');
$dt->setTimezone(new DateTimeZone('Asia/Karachi'));
echo $dt->format('Y-m-d H:i:s'); // 2024-05-11 17:18:32
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('Y-m-d H:i:s'); // 2024-05-11 08:18:32Build the DateTime from the timestamp with the at sign, which always starts in UTC, then call setTimezone with the zone you want. The conversion handles daylight saving by the real rules, so 1715429912 reads as 17:18:32 in Karachi and 08:18:32 in New York, both the same instant. The timezone converter on this site shows the same instant in 12 zones at once, so you can check a setTimezone result against it in seconds.
Common PHP Timestamp Mistakes
- Trusting the server default zone. date() and strtotime() use it silently. Set it explicitly with date_default_timezone_set('UTC') at the start of your code.
- Using strtotime() on user input without a zone. A date string with no zone is read in the default zone. Ask for the zone, or require UTC.
- Storing dates as text without a zone. A bare 2024-05-11 12:18:32 in a database cannot be converted back reliably. Store the timestamp, or store the string with its offset.
- Comparing a millisecond value with time(). A 13 digit JavaScript timestamp is a thousand times larger than time(), so every comparison says expired. Divide by 1000 first.
- Assuming date() returns false safely. On 32 bit PHP, dates past 2038 fail. Check the platform before trusting dates past 19 January 2038 in old code.
PHP Timestamp FAQs
How do I get the current unix timestamp in PHP?
Use time(). It returns the current time in seconds since the epoch, as an integer, which is the unix timestamp itself. If you need fractions, microtime(true) returns the same instant as a float with microseconds included. Both read the same clock, so they agree with each other and with the live converter above.
How do I convert a unix timestamp to a date in PHP?
Use date() with a format string: date('Y-m-d H:i:s', 1715429912) gives 2024-05-11 12:18:32 in the default time zone, so set the zone to UTC first with date_default_timezone_set('UTC'). For more control, use DateTime: new DateTime('@1715429912') builds a DateTime in UTC directly, and format('Y-m-d H:i:s') prints it. The at sign tells the constructor the value is a timestamp, not a date string.
How do I convert a date to a unix timestamp in PHP?
Use strtotime() for a quick conversion, or DateTime for a careful one. strtotime('2024-05-11 12:18:32 UTC') gives 1715429912. The DateTime way is new DateTime('2024-05-11 12:18:32', new DateTimeZone('UTC')), then getTimestamp(), which gives the same 1715429912. The DateTime way is better when the input zone matters, because the zone is explicit instead of depending on the server default.
What time zone does PHP use by default?
Whatever the server is set to, from php.ini or from date_default_timezone_set(). That is the trap: the same code gives a different timestamp in Karachi than in London if the default differs. Always set the zone explicitly at the start of your code, with date_default_timezone_set('UTC') for UTC, or pass a DateTimeZone to every DateTime you build. Never let a timestamp depend on a server setting you did not choose.
What is the difference between time() and microtime() in PHP?
time() returns whole seconds since the epoch, the unix timestamp. microtime() without an argument returns a string with microseconds and seconds in an awkward order, and microtime(true) returns a float with the fractions included. Use time() for storing, comparing and sending, and microtime(true) only when you are measuring how long something took.
How do I format a unix timestamp for a time zone in PHP?
Build a DateTime from the timestamp and set its zone: $dt = new DateTime('@1715429912'); $dt->setTimezone(new DateTimeZone('Asia/Karachi')); then $dt->format('Y-m-d H:i:s') gives 2024-05-11 17:18:32. The at sign constructor always starts in UTC, so setTimezone after it does the conversion properly, with daylight saving handled by the real rules.
Does PHP handle the year 2038 problem?
On 64 bit PHP, yes. time() and DateTime use 64 bit integers, so dates far past 2038 work fine, and the timestamp 2147483647, which is 19 January 2038 at 03:14:07 UTC, is just an ordinary value. On 32 bit PHP, dates past that point overflow, and date() returns false or a wrong date. Every modern server runs 64 bit PHP, so this is a legacy problem, but check the platform before trusting dates past 2038 in old code.
How do I check if a unix timestamp is expired in PHP?
Compare it with time(): time() > $exp means the token or session is expired. Both sides are seconds since the epoch, so the comparison is plain integer arithmetic. That is the same check the JWT decoder on this site does for the exp claim. Make sure the value you compare is really in seconds: a millisecond value from JavaScript is a thousand times larger and makes everything look expired.
Related tools
Source: PHP manual, date and time functions. Reference values on this page were checked against the PHP documentation on 27 November 2026.