PHP: convert negative timestamp

I have a negative timestamp, and I wanted to convert it to a readable format.

$timestamp = -1861945262080;

If I use date("d-m-Y", $timestamp), it will simply output 12-08-2035.

+2
source share
1 answer

Below code snippet converts your UNIX timestamp to a valid date-month-year. However , as shown below. passing pretty large negative unix timestamps can produce unexpected results

 <?php
    $dt = new DateTime();
    $dt->setTimestamp(-1861945262080); //<--- Pass a UNIX TimeStamp
    echo $dt->format('d-m-Y');

OUTPUT :

12-08-2035

However, you can still pass negative timestamps above. Consider this passage from wikipedia.

Unix Unix 86400 . , 2004-09-16T00: 00: 00Z, 12677 , Unix 12677 × 86400 = 1095292800. , ; , 1957-10-04T00: 00: 00Z, 4472 , Unix -4472 × 86400 = -386380800.

, -386380800 .

 <?php
    $dt = new DateTime();
    $dt->setTimestamp(-386380800); //<--- Pass a UNIX TimeStamp
    echo $dt->format('d-m-Y');

OUTPUT :

04-10-1957

.

+3

Source: https://habr.com/ru/post/1673272/


All Articles