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?
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);
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.
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.
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”.
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
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?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With