Calculate time ago in PHP

<?php //$time is two hours behind the current system time $time = time() - (3600 * 2); getTimeAgoCalculation($time); //Future time getTimeAgoCalculation(time() + 1); //5 days 4 hours ago and 8 minutes $days = 5 * (3600 * 24); $hours = 4 * 3600; $minutes = 8 * 60; getTimeAgoCalculation(time() - ($days + $hours + $minutes)); function getTimeAgoCalculation($time) { $difference = time() - $time; if($difference <= 0) { echo "The time is in the future!"; echo "<br>"; return; } //Round the amount of days down $days = round($difference / (3600 * 24)); //Remove the amount of seconds during the amount of days $difference -= $days * (3600 * 24); //Round the amount of hours down $hours= round($difference / 3600); //Remove the amount of seconds during the amount of hours $difference -= $hours * 3600; //Round the amount of hours down $minutes= round($difference / 60); //Remove the amount of seconds during the amount of hours $difference -= $minutes * 60; echo $days ." days, " . $hours. " hours and " . $minutes . " minutes ago."; echo "<br>"; } ?>
You can calculate the amount of time that has passed from the current time and a specific time.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.