|
Display Time Left To a Date
A PHP function to display the time between now and a future date supplied as Unix time, in human readable format, using two blocks of time periods (year, month, week, day, hour or minute.)
|
On Friday, July 31st 2009 at 09:30 PM By Andrew Pociu (View Profile)
    (Rated 4.7 with 3 votes) |
Contextual Ads
More PHP Resources
Advertisement
function TimeTo($future) // $original should be the future date and time in unix format
{
// Common time periods as an array of arrays
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
);
$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}s";
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}s";
}
}
return $print;
}
|
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|
Rate this code snippet
Current Comments
There are no comments.
|
Related Source Code
There is no related code.
Related Tutorials
There are no related tutorials.
PHP Job Search
|
|
|