How to convert epoch / UNIX timestamps to normal readable date/time using PHP.
- Getting current epoch time in PHP
- Converting from epoch to normal date in PHP
- Converting from normal date to epoch in PHP
- Convert time zones in PHP
- Comments
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:
| format | output |
| r | Sat, 15 Mar 2014 12:00:00 +0100 (RFC 2822 date) |
| c | 2014-03-15T12:00:00+01:00 (ISO 8601 date, PHP 5+) |
| M/d/Y | Mar/15/2014 |
| d/m/y | 15-03-2014 |
| Y-m-d H:i:s | 2014-03-15 12:00:00 |
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";
$TimeZoneNameFrom="UTC";
$TimeZoneNameTo="Europe/Amsterdam";
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
blog comments powered by DisqusPages
Tools
Epoch converter
Batch converter
Epoch clock
Epoch list
Time zone converter
LDAP converter
Unix hex timestamp
Mac timestamp
Bin/Oct/Hex converter
Programming
Routines by language
Epoch in C
Epoch in MySQL
Epoch in PERL
Epoch in PHP
Epoch in VBScript/ASP/JavaScript
Calculate difference
between two dates
Week numbers
Weeks by year
Day numbers
Days by year
More
Comments & questions
Este sitio en Español
Related cookbooks
Unicode Tools
Character Set Tools
Follow us
