Home >Backend Development >PHP Tutorial >Why is my PHP code converting milliseconds to an incorrect date?

Why is my PHP code converting milliseconds to an incorrect date?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-03 08:22:30980browse

Why is my PHP code converting milliseconds to an incorrect date?

php: Convert Milliseconds to Date Accuracy

When converting a string representing milliseconds since the Unix epoch to a date in d-m-Y format, it's crucial to ensure the accuracy of the conversion. Despite the code provided, the result obtained may not match the expected outcome due to a misunderstanding of the given string.

The code:

<code class="php">$mil = 1227643821310;
$seconds = $mil / 1000;
echo date("d-m-Y", $seconds);</code>

correctly converts the milliseconds to seconds, but the original value, "1227643821310," corresponds to 25-11-2008, not 02-12-2008. This explains the discrepancy in the output.

The above is the detailed content of Why is my PHP code converting milliseconds to an incorrect date?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn