Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a unix timestamp to a human readable date in 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?

like image 605
Joseph U. Avatar asked Apr 19 '12 17:04

Joseph U.


People also ask

How to format a Unix timestamp in PHP?

We can use the date function to format a Unix Timestamp in PHP. To the ISO 8601 format – $formatted = date (DateTimeInterface::ATOM, $UNIX); To MySQL date format – $formatted = date ("Y-m-d H:i:s", $UNIX); Various other custom formats – $formatted = date ("D, j F Y h:i:s A", $UNIX);

How to convert a timestamp to a human readable date or time?

The date () function converts a timestamp to a human readable date or time. The correct syntax to use this function is as follows It has two parameters. The parameter $format is the date-time format that the timestamp is converted to.

How to convert Unix time to Epoch time in PHP?

As you probably already know, “Unix Time” is the number of seconds that have passed since the 1st of January, 1970. For the sake of this example, let’s say that we want to convert 2019-04-01 10:32:00 into an Epoch timestamp. To do this in PHP, we can use the strtotime function like so: //Our date string.

How to display PHP date time in formatted form?

First we will get unix timestamp and then convert it into PHP date time using PHP DateTime class. $unix_timestamp = $_POST['timestamp']; $datetime = new DateTime("@$unix_timestamp"); Step2: Display PHP Date Time in Formatted Form. Now we will display PHP Date Time in formatted form like “02-10-2016 21:05:18”.


2 Answers

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
like image 105
ThiefMaster Avatar answered Sep 19 '22 18:09

ThiefMaster


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

<?php date_default_timezone_set("Australia/Perth"); ?>

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

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

like image 20
Brad Farleigh Avatar answered Sep 19 '22 18:09

Brad Farleigh