PHP Epoch Converter and Date/Time Routines

How to convert epoch / UNIX timestamps to normal readable date/time using PHP.

Getting current epoch time in PHP

Time returns an integer with the current epoch:

time()  // current Unix timestamp 
microtime(true) // microtime returns timestamp with microseconds (param: true=float, false=string)

Convert from epoch to human readable date in PHP

1. Use the 'date' function.

$epoch = 1340000000;
echo date('r', $epoch); // output as RFC 2822 date - returns local time
echo gmdate('r', $epoch); // returns GMT/UTC time

You can use the time zone code below (date_default_timezone_set) to switch time zone of the input date.

2. Use the DateTime class.

$epoch = 1344988800; 
$dt = new DateTime("@$epoch");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00 

The DateTime class is PHP 5+ only.

In the examples above "r" and "Y-m-d H:i:s" are PHP date formats, other examples:

rSat, 15 Mar 2014 12:00:00 +0100 (RFC 2822 date)
c2014-03-15T12:00:00+01:00 (ISO 8601 date, PHP 5+)
Y-m-d H:i:s  2014-03-15 12:00:00

Complete list of format options


Convert from human readable date to epoch in PHP

There are many options:

1. Using 'strtotime':

strtotime parses most English language date texts to epoch/Unix Time.

echo strtotime("15 November 2012");
// ... or ...
echo strtotime("2012/11/15");
// ... or ...
echo strtotime("+10 days"); // 10 days from now
It's important to check if the conversion was successful:
// PHP 5.1.0 or higher, earlier versions check: strtotime($string)) === -1
if ((strtotime("this is no date")) === false) {
   echo 'failed';

2. Using the DateTime class:

The PHP 5 DateTime class is nicer to use:

// object oriented
$date = new DateTime('01/15/2010'); // format: MM/DD/YYYY
echo $date->format('U'); 
// or procedural $date = date_create('01/15/2010'); echo date_format($date, 'U');

The date format 'U' converts the date to a UNIX timestamp.

3. Using 'mktime':

This version is more of a hassle but works on any PHP version.

// PHP 5.1+ 
date_default_timezone_set('UTC');  // optional 
mktime ( $hour, $minute, $second, $month, $day, $year );
// before PHP 5.1 mktime ( $hour, $minute, $second, $month, $day, $year, $is_dst ); // $is_dst : 1 = daylight savings time (DST), 0 = no DST , -1 (default) = auto  
// example: generate epoch for Jan 1, 2000 (all PHP versions) echo mktime(0, 0, 0, 1, 1, 2000);

All these PHP routines can't handle dates before 13 December 1901.

Set your timezone

If you are using PHP 5.1 or higher use date_default_timezone_set to set/overrule your timezone.
The PHP time zone handling takes care of daylight saving times (list of PHP time zones).


Convert date/time to another time zone

$TimeStr="2012-01-01 12:00:00";
echo date_create($TimeStr, new DateTimeZone($TimeZoneNameFrom))
		->setTimezone(new DateTimeZone($TimeZoneNameTo))->format("Y-m-d H:i:s");

List of all time zones in PHP
Thanks to Albert Scholtalbers.

Comments and questions

« Epoch Converter Functions


Date and Time
Week numbers
Weeks by year
Day numbers
Days by year
Calculate difference
   between two dates

Comments & questions
Este sitio en Español
Related cookbooks
Charset Tools

Follow us