Timestamp to human date php

I have a timestamp value from PHP: 1188604800000

When I format the time to human readable like this:

date["m/d/Y", 1188604800000]

It prints:

05/21/39635

If I put the number into an online Unix Timestamp converter I get:

Sat, 01 Sep 2007 00:00:00 GMT

What am I doing wrong?

asked Apr 19, 2012 at 17:49

PHP uses seconds-based timestamps, so divide 1188604800 by 1000 and you are good.

php> echo date['Y-m-d', 1188604800000/1000];
2007-09-01

answered Apr 19, 2012 at 17:51

ThiefMasterThiefMaster

302k78 gold badges581 silver badges625 bronze badges

1

I was having trouble with my date being one day off and I had to manually set the default timezone to match my location by using


A list of support timezones can be found here - //www.php.net/manual/en/timezones.php

[I don't have enough rep to comment so can someone merge that with the actual answer?]

answered Jul 20, 2012 at 8:17

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:

FormatOutput
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

« Epoch Converter Functions

How do I change timestamp to date?

Let's see the simple example to convert Timestamp to Date in java..
import java.sql.Timestamp;.
import java.util.Date;.
public class TimestampToDateExample1 {.
public static void main[String args[]]{.
Timestamp ts=new Timestamp[System.currentTimeMillis[]];.
Date date=new Date[ts.getTime[]];.
System.out.println[date];.

What time function will convert the timestamp to a human readable format?

Use date[] Function to Convert a Timestamp to a Date/Time in PHP.

What is epoch in PHP?

Definition and Usage. The time[] function returns the current time in the number of seconds since the Unix Epoch [January 1 1970 00:00:00 GMT].

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 [seconds in a day] floored and add 719163 [the days up to the year 1970] to pass to it. Awesome, this is as manual as it gets.

Chủ Đề