← Back to Articles

Unix Timestamps in PHP: time(), strtotime(), and DateTime

PHP provides a compact set of functions for working with Unix epoch values, from simple timestamps to formatted date output and timezone-aware date objects.

Get the current Unix timestamp

Use time() for second precision and microtime() when you need fractional seconds.

// Current Unix timestamp in seconds
echo time();

// Current timestamp with microseconds
echo microtime(true);
AdSense — In-Article Banner

Convert a date string to timestamp

The strtotime() function converts readable date strings into epoch seconds.

// Convert a date string to Unix timestamp
$timestamp = strtotime('2026-05-24 15:30:00');
echo $timestamp;

// Convert with a timezone offset
$timestamp = strtotime('2026-05-24 15:30:00 UTC');
echo $timestamp;

Convert timestamp to readable date

Use date() for formatted strings or DateTime::createFromFormat() for object-based manipulation.

// Format a Unix timestamp
echo date('Y-m-d H:i:s', 1716584400);

// Create a DateTime object from a timestamp
$date = DateTime::createFromFormat('U', 1716584400);
echo $date->format('Y-m-d H:i:s');
AdSense — In-Article Banner