Jump to content

Two timestamps, detailed breakdown of distance.


Ninjakreborn

Recommended Posts

Getting the difference in seconds between two timestamps is easy.  Taking the seconds and doing a breakdown of time frames is also easy.

<?php

echo '<p>There are a total of ' . round($seconds) . ' seconds between Timestamp 1 and Timestamp 2.</p>';
echo '<p>There are a total of ' . round($seconds / 60) . ' minutes between Timestamp 1 and Timestamp 2.</p>';
echo '<p>There are a total of ' . round(($seconds / 60) / 1440) . ' days between Timestamp 1 and Timestamp 2.</p>';
echo '<p>There are a total of ' . round((($seconds / 60) / 1440) / 7) . ' weeks between Timestamp 1 and Timestamp 2.</p>';
echo '<p>There are a total of ' . round((((($seconds / 60) / 1440) / 7) / 30)) . ' months between Timestamp 1 and Timestamp 2.</p>';
echo '<p>The total breakdown of time from Timestamp 1 to Timestamp 2 is .</p>';
?>

What I am trying to figure out, is get a collective amount as well....

X years X months X weeks X days X minutes and X seconds.

Does anyone have any good algorithm for handling that, or have any feedback on where to start to handle this type of math.

Nevermind, I got a good breakdown.

<?php
$diff = $seconds;	
$years   = floor($diff / (365*60*60*24)); 
$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); 

$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); 

$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60)); 
echo $years . ' Years, ' . $months . ' Months, ' . $days . ' Days, ' . $hours . ' Hours, ' . $minuts . ' Minutes, and ' . $seconds . ' Seconds.</p>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.