PHP Unix Timestamp Guide

Complete guide to working with Unix timestamps in PHP. Learn how to get current epoch time, convert dates to timestamps, and convert timestamps to readable dates using PHP functions.

PHP provides excellent built-in functions for working with Unix timestamps. The time() function and date() function are the most commonly used, along with the DateTime class for more advanced operations. This guide covers all essential timestamp operations in PHP.

How to get the current epoch time

PHP
time()
php

More PHP

Convert from human-readable date to epoch

PHP
strtotime("15 November 2018")
php

(converts most English date texts) or: date_create('11/15/2018')->format('U') (using DateTime class) More PHP

Convert from epoch to human-readable date

PHP
date(output format, epoch);
php

Output format example: 'r' = RFC 2822 date, more PHP examples

Additional PHP Examples

Using DateTime Class

<?php
// Create DateTime from epoch
$date = new DateTime();
$date->setTimestamp(1609459200);
echo $date->format('Y-m-d H:i:s');

// Convert date string to epoch
$date = new DateTime('2024-01-01 12:00:00');
echo $date->getTimestamp();
php

The DateTime class provides object-oriented interface for date/time operations.

Formatting Options

<?php
$epoch = 1609459200;

// RFC 2822 format
echo date('r', $epoch);

// ISO 8601 format
echo date('c', $epoch);

// Custom format
echo date('Y-m-d H:i:s', $epoch);
php

PHP's date() function supports many format specifiers for custom date formatting.

About PHP Unix Timestamps

PHP provides excellent built-in functions for working with Unix timestamps. The time() function and date() function are the most commonly used, along with the DateTime class for more advanced operations. PHP makes it easy to get current timestamps, convert between timestamps and dates, and format dates in various ways. This guide covers all essential timestamp operations in PHP.

Related guides: Check out our guides for other programming languages: JavaScript, Python, Java, and more. For timestamp conversion tools, visit our Tools page.

Frequently Asked Questions

How do I get the current Unix timestamp in PHP?

Use time() to get the current Unix timestamp in seconds, or microtime(true) for microsecond precision. The time() function is the most commonly used method for getting Unix timestamps in PHP.

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

Use date() function with a format string: date("Y-m-d H:i:s", $timestamp). For more advanced operations, use the DateTime class: new DateTime("@$timestamp") or DateTime::createFromFormat("U", $timestamp).

What is the difference between time() and strtotime()?

time() returns the current Unix timestamp, while strtotime() converts a date string to a Unix timestamp. Use time() for current time, and strtotime() for parsing date strings like "2024-01-01" or "next Monday".

Related Guides