Home > Net >  Unix epoch time converts to human readable incorrectly with PHP
Unix epoch time converts to human readable incorrectly with PHP

Time:09-17

1630440104 is the Unix epoch time code example.

This PHP code

echo date('Y.m.d', '1630440104').' at '.date('H:m:s', '1630440104');

outputs it as 2021.08.31 at 23:08:44 which is incorrect.

Because the correct output should be 2021.08.31 at 23:01:44.

Whatever time code I insert into the above PHP code it returns 08 minutes instead of the actual value.

Where have I made a mistake?

CodePudding user response:

The mistake is using m in the second date call. m is month, minutes is i.

echo date('Y.m.d', 1630440104).' at '.date('H:i:s', 1630440104);

or

echo date('Y.m.d \a\t H:i:s', 1630440104);

Just a side note, date second parameter accepts an int not a string in declare(strict_types=1); a TypeError would occur.

You can find the available formats under DateTime::format docs page.

  • Related