Current Location: Home> Latest Articles> How to Use the gmmktime Function to Calculate the Difference Between Two Time Points? A Step-by-Step Guide

How to Use the gmmktime Function to Calculate the Difference Between Two Time Points? A Step-by-Step Guide

gitbox 2025-09-18
<span><span><span class="hljs-meta"><?php</span></span><span>  
</span><span><span class="hljs-comment">// Irrelevant content before the article</span></span><span>  
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"This is a piece of text not related to the main content."</span></span><span>;  
<p></span>// Divider<br>
echo "<hr>";<br>
?></p>
<p><h1>How to Use the gmmktime Function to Calculate the Difference Between Two Time Points? A Step-by-Step Guide</h1></p>
<p><p>In PHP, <code>gmmktime<span>()

3. Calculate the Difference in Seconds


By subtracting the two Unix timestamps, we get the difference in seconds:

&lt;?php  
$diff = $time2 - $time1;  
echo "The difference between the two times is: $diff seconds";  
?&gt;  

4. Convert to Days


If we care more about days, we can convert the seconds into days:

&lt;?php  
$days = $diff / (60 * 60 * 24);  
echo "The difference between the two times is $days days";  
?&gt;  

5. Full Example


Here’s the complete code:

&lt;?php  
$time1 = gmmktime(0, 0, 0, 1, 1, 2023);  
$time2 = gmmktime(0, 0, 0, 2, 1, 2023);  

$diff = $time2 - $time1;  
$days = $diff / (60 * 60 * 24);  

echo "From 2023-01-01 to 2023-02-01, the difference is:&lt;br&gt;";  
echo "$diff seconds&lt;br&gt;";  
echo "$days days";  
?&gt;  

6. Conclusion


With gmmktime(), we can easily get GMT timestamps and calculate the difference between two time points by subtraction. After conversion, we can obtain values in seconds, minutes, hours, or even days. This makes it very handy for time difference calculations. If you encounter a similar need in your project, consider using this approach.