| 0 comments ]

//function to find out time finished in words

function timeFinishedInWords($datetime_string, $format = 'j/n/y', $backwards = false, $return = false) {
$datetime = $this->fromString($datetime_string);

$inSeconds = $datetime;
if ($backwards) {
$diff = $inSeconds - time();
} else {
$diff = time() - $inSeconds;
}

$months = floor($diff / 2419200);
$diff -= $months * 2419200;
$weeks = floor($diff / 604800);
$diff -= $weeks * 604800;
$days = floor($diff / 86400);
$diff -= $days * 86400;
$hours = floor($diff / 3600);
$diff -= $hours * 3600;
$minutes = floor($diff / 60);
$diff -= $minutes * 60;
$seconds = $diff;

if ($months > 0) {

$relativeDate = 'on ' . date($format, $inSeconds);
$old = true;
} else {
$relativeDate = '';
$old = false;

if ($weeks > 0) {
// weeks and days
$relativeDate .= ($relativeDate ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : '');
$relativeDate .= $days > 0 ? ($relativeDate ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : '';
} elseif ($days > 0) {
// days and hours
$relativeDate .= ($relativeDate ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '');
$relativeDate .= $hours > 0 ? ($relativeDate ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : '';
} elseif ($hours > 0) {
// hours and minutes
$relativeDate .= ($relativeDate ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '');
$relativeDate .= $minutes > 0 ? ($relativeDate ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : '';
} elseif ($minutes > 0) {
// minutes only
$relativeDate .= ($relativeDate ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '');
} else {
// seconds only
$relativeDate .= ($relativeDate ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : '');
}
}

$ret = $relativeDate;

// show relative date and add proper verbiage
if (!$backwards && !$old) {
$ret .= ' ago';
}
return $this->output($ret, $return);
}

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.