Home >Backend Development >PHP Tutorial >How do I convert milliseconds to a date in PHP?

How do I convert milliseconds to a date in PHP?

DDD
DDDOriginal
2024-11-02 12:05:03562browse

How do I convert milliseconds to a date in PHP?

Convert Milliseconds to Date in PHP

In PHP, you can convert milliseconds since the Unix epoch to a date string using the date() function. However, it's essential to consider the correct value of milliseconds for the targeted date.

You mentioned that your code is converting "1227643821310" milliseconds to "2-12-2008" in d-m-Y format. However, after checking, it turns out that the provided milliseconds correspond to "25-11-2008" when converted.

To ensure accuracy, remember that milliseconds represent the number of milliseconds since the Unix epoch (January 1, 1970 UTC). In the example provided, the milliseconds you specified translate to November 25, 2008 at 08:10:21 UTC, which aligns with the result "25-11-2008."

To convert milliseconds to a date in PHP, you can use the following code:

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

This code will print "25-11-2008," which is the correct date for the provided milliseconds.

The above is the detailed content of How do I convert milliseconds to a date in PHP?. 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