Based on @Talon i decided to go a little further and write a small class:
/**
* Calculates the time between two UNIX timestamps
* Like 3 hours or 180 minutes.
*
* @package Add Package Name Here
* @version 1
* @since Jul 12, 2013
* @author Andrew Starlike <andrewstarlike@gmail.com>
*/
class NiceDate
{
private $translation;
/**
* @param array $translation
*/
public function __construct($translation = null)
{
$this->setTranslation($translation);
}
/**
* Sets the translation of the time string
*
* @param array $translation (the array must have this keys 'second', 'minute', 'hour', 'day', 'month', 'year')
*/
private function setTranslation($translation = null)
{
if ($translation === null) {
$translation = array();
$plural = 's';
$formats = array('second', 'minute', 'hour', 'day', 'month', 'year');
foreach ($formats as $format) {
$translation[$format] = ucfirst($format);
$translation[$format . $plural] = ucfirst($format . $plural);
}
}
$this->translation = $translation;
}
/**
*
* @param int $old (UNIX timestamp)
* @param int $newer (UNIX timestamp)
* @return string
*/
public function timeLeft($old, $newer)
{
$datetime1 = new DateTime('@' . $newer);
$datetime2 = new DateTime('@' . $old);
$interval = $datetime1->diff($datetime2);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%a');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%S');
$translation = $this->translation;
if ($seconds):
$elapsed = $seconds . ' ';
$elapsed .= $seconds == 1 ? $translation['second'] : $translation['seconds'];
endif;
if ($minutes):
$elapsed = $minutes . ' ';
$elapsed .= $minutes == 1 ? $translation['minute'] : $translation['minutes'];
endif;
if ($hours):
$elapsed = $hours . ' ';
$elapsed .= $hours == 1 ? $translation['hour'] : $translation['hours'];
endif;
if ($days):
$elapsed = $days . ' ';
$elapsed .= $days == 1 ? $translation['day'] : $translation['days'];
endif;
if ($months):
$elapsed = $months . ' ';
$elapsed .= $months == 1 ? $translation['month'] : $translation['months'];
endif;
if ($years):
$elapsed = $years . ' ';
$elapsed .= $years == 1 ? $translation['year'] : $translation['years'];
endif;
return $elapsed;
}
}