I have a negative timestamp and I wanted to convert it to a readable date format.
$timestamp = -1861945262080;
If I use date("d-m-Y", $timestamp)
, it will just output 12-08-2035.
I have a negative timestamp and I wanted to convert it to a readable date format.
$timestamp = -1861945262080;
If I use date("d-m-Y", $timestamp)
, it will just output 12-08-2035.
The below piece of code transforms your UNIX timestamp to a valid date-month-year. However , passing pretty large negative unix timestamps can produce unexpected results
as shown below.
<?php
$dt = new DateTime();
$dt->setTimestamp(-1861945262080); //<--- Pass a UNIX TimeStamp
echo $dt->format('d-m-Y');
OUTPUT :
12-08-2035
However, you still can pass negative timestamps to the above thing. Consider this excerpt from wikipedia
.
The Unix time number is zero at the Unix epoch, and increases by exactly 86400 per day since the epoch. Thus 2004-09-16T00:00:00Z, 12677 days after the epoch, is represented by the Unix time number 12677 × 86400 = 1095292800. This can be extended backwards from the epoch too, using negative numbers; thus 1957-10-04T00:00:00Z, 4472 days before the epoch, is represented by the Unix time number -4472 × 86400 = -386380800.
So let's pass the -386380800
to the above code.
<?php
$dt = new DateTime();
$dt->setTimestamp(-386380800); //<--- Pass a UNIX TimeStamp
echo $dt->format('d-m-Y');
OUTPUT :
04-10-1957
which is the expected output as per the sources.