Created
November 22, 2022 10:56
-
-
Save madindo/176bbe9b184283e7bc3bf804b385b3f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function timeLeft($future) // $original should be the future date and time in unix format | |
{ | |
date_default_timezone_set('Asia/Jakarta'); | |
// Common time periods as an array of arrays | |
$periods = array( | |
array(60 * 60 * 24 * 365 , 'Y'), | |
array(60 * 60 * 24 * 30 , 'M'), | |
array(60 * 60 * 24 * 7, 'W'), | |
array(60 * 60 * 24 , 'D'), | |
array(60 * 60 , 'H'), | |
array(60 , 'M'), | |
); | |
$today = time(); | |
$since = $future - $today; // Find the difference of time between now and the future | |
// Loop around the periods, starting with the biggest | |
for ($i = 0, $j = count($periods); $i < $j; $i++) | |
{ | |
$seconds = $periods[$i][0]; | |
$name = $periods[$i][1]; | |
// Find the biggest whole period | |
if (($count = floor($since / $seconds)) != 0) | |
{ | |
break; | |
} | |
} | |
$print = ($count == 1) ? '1 '.$name : "$count {$name}"; | |
if ($i + 1 < $j) | |
{ | |
// Retrieving the second relevant period | |
$seconds2 = $periods[$i + 1][0]; | |
$name2 = $periods[$i + 1][1]; | |
// Only show it if it's greater than 0 | |
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) | |
{ | |
$print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}"; | |
} | |
} | |
return $print; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment