PHP Epoch Converter and Date/Time Routines
How to convert epoch / UNIX timestamps to normal readable date/time using PHP 7.
- 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
- Adding years/months to a date 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 = 1483228800;
echo date('r', $epoch); // output as RFC 2822 date - returns local time
echo gmdate('r', $epoch); // returns GMT/UTC time: Sun, 01 Jan 2017 00:00:00 +0000
You can use the time zone code below (date_default_timezone_set) to switch the time zone of the input date.
2. Use the DateTime class.
$epoch = 1483228800;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2017-01-01 00:00:00
In the examples above "r" and "Y-m-d H:i:s" are PHP date formats, other examples:
Format | Output |
---|---|
r | Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date) |
c | 2017-03-15T12:00:00+01:00 (ISO 8601 date) |
M/d/Y | Mar/15/2017 |
d-m-Y | 15-03-2017 |
Y-m-d H:i:s | 2017-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 2017");
// ... or ...
echo strtotime("2017/11/15");
// ... or ...
echo strtotime("+10 days"); // 10 days from now
It's important to check if the conversion was successful:
if ((strtotime("this is no date")) === false) {
echo 'failed';
}
2. Using the DateTime class:
The PHP DateTime class is nicer to use:
// object oriented
$date = new DateTime('01/15/2017'); // format: MM/DD/YYYY
echo $date->format('U');
// or procedural
$date = date_create('01/15/2017');
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 );
// 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
Use date_default_timezone_set to set/overrule your timezone.
The PHP time zone handling takes care of daylight saving times (list of available time zones).
Examples:
date_default_timezone_set('Europe/Amsterdam');
date_default_timezone_set('America/New York');
date_default_timezone_set('EST');
date_default_timezone_set('UTC');
Convert date/time to another time zone
$TimeStr="2017-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 time zones in PHP
Thanks to Albert Scholtalbers.
Adding or subtracting years/months to a date
With strtotime
you can easily add or subtract years/months/days/hours/minutes/seconds to a date.
$currentDate = time(); // get current date
echo "It is now: ".date("Y-m-d H:i:s", $currentDate)."\n ";
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +1 year"); // add 1 year to current date
echo "Date in epoch: ".$date."\n ";
echo "Readable date: ".date("Y-m-d H:i:s",$date)."\n ";
Other examples:
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +1 month"); // add 1 month to a date
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +6 months"); // add 6 months
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " +1 day"); // add 1 day
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " -12 hours"); // subtract 12 hours
$date = strtotime(date("Y-m-d H:i:s", $currentDate) . " -1 day -12 hours"); // subtract 1 day and 12 hours