Add two date helpers

- time_diff() to count the seconds between two datetimes
- secondsToString() to make a pretty string from seconds
This commit is contained in:
Karsten Heiken
2011-09-09 21:08:10 +02:00
parent 1b34fded3b
commit f69f56597a

View File

@@ -35,5 +35,68 @@ if ( ! function_exists('mysql_now'))
} }
} }
/**
* Function to calculate date or time difference.
*
* Function to calculate date or time difference. Returns an array or
* false on error.
*
* @author J de Silva <giddomains@gmail.com>
* @copyright Copyright &copy; 2005, J de Silva
* @link http://www.gidnetwork.com/b-16.html Get the date / time difference with PHP
* @param string $start
* @param string $end
* @return array
*/
if ( ! function_exists('time_diff'))
{
function time_diff($start, $end)
{
$uts['start'] = strtotime($start);
$uts['end'] = strtotime($end);
if ($uts['start'] !== -1 && $uts['end'] !== -1) {
if ($uts['end'] >= $uts['start']) {
$diff = $uts['end'] - $uts['start'];
$diff = intval($diff);
return ($diff);
}
}
return (false);
}
}
/**
* Function to convert seconds to a pretty string.
*
* @author Karsten Heiken <karsten@disposed.de>
* @param integer $secs the amount of seconds
* @param boolean $includeseconds should seconds be appended to the string?
* @return string
*/
if ( ! function_exists('secondsToString'))
{
function secondsToString($secs, $includeseconds = false)
{
$days = intval($secs / 86400);
$hours = intval($secs / 3600 % 24);
$minutes = intval($secs / 60 % 60);
$seconds = intval($secs % 60);
if (($minutes + $hours + $days) < 1)
return (sprintf(_('%d seconds'), $seconds));
else if (($minutes + $hours) < 1)
$string = sprintf(_('%d minutes'), $minutes);
else if ($days < 1)
$string = sprintf(_('%d hours, %d minutes'), $hours, $minutes);
else
$string = sprintf(_('%d days, %d hours, %d minutes'), $days, $hours,
$minutes);
if ($includeseconds)
$string .= ' ' . sprintf(_('and %d seconds'), $seconds);
return $string;
}
}
/* End of file MY_date_helper.php */ /* End of file MY_date_helper.php */
/* Location: ./application/helpers/MY_date_helper.php */ /* Location: ./application/helpers/MY_date_helper.php */