I used DateInterval to convert seconds into years, months, days, hours, minutes and seconds.
<?php
function convertSecondsToEnglish($num_seconds)
{
if (!is_numeric($num_seconds) || $num_seconds < 0) {
throw new Exception('Number of seconds must be greater than zero.');
}
$now = new DateTime();
$future = new DateTime();
$future->modify(sprintf('+%d second', $num_seconds));
$diff = $future->diff($now);
$components = [];
// years
if ($diff->y != 0) {
$years = $diff->y;
$components[] = sprintf('%d year%s', $years, ($years != 1) ? 's' : '');
}
// months
if ($diff->m != 0) {
$months = $diff->m;
$components[] = sprintf('%d month%s', $months, ($months != 1) ? 's' : '');
}
// days
if ($diff->d != 0) {
$days = $diff->d;
$components[] = sprintf('%d day%s', $days, ($days != 1) ? 's' : '');
}
// hours
if ($diff->h != 0) {
$hours = $diff->h;
$components[] = sprintf('%d hour%s', $hours, ($hours != 1) ? 's' : '');
}
// minutes
if ($diff->i != 0) {
$mins = $diff->i;
$components[] = sprintf('%d minute%s', $mins, ($mins != 1) ? 's' : '');
}
// seconds
if ($diff->s != 0) {
$seconds = $diff->s;
$components[] = sprintf('%d second%s', $seconds, ($seconds != 1) ? 's' : '');
}
return implode(', ', $components);
}
Example:
echo convertSecondsToEnglish(3600) . PHP_EOL;
echo convertSecondsToEnglish(32) . PHP_EOL;
echo convertSecondsToEnglish(60) . PHP_EOL;
echo convertSecondsToEnglish(1234567) . PHP_EOL;
echo convertSecondsToEnglish(12345678) . PHP_EOL;
echo convertSecondsToEnglish(87654321) . PHP_EOL;
Gives:
1 hour
32 seconds
1 minute
14 days, 6 hours, 56 minutes, 7 seconds
4 months, 19 days, 21 hours, 21 minutes, 18 seconds
2 years, 9 months, 9 days, 12 hours, 25 minutes, 21 seconds
Demo: https://eval.in/822061
EDIT:
To use the word 'and' between words:
$to_return = '';
if (count($components) > 2) {
$last_item = array_pop($components);
$to_return = implode(', ', $components);
$to_return .= ' and ' . $last_item;
} else {
$to_return = implode(' and ', $components);
}
return $to_return;
Gives:
1 hour
32 seconds
1 minute
1 minute and 30 seconds
14 days, 6 hours, 56 minutes and 7 seconds
4 months, 19 days, 21 hours, 21 minutes and 18 seconds
2 years, 9 months, 9 days, 12 hours, 25 minutes and 21 seconds
Demo of this: https://eval.in/822330
Hope this helps :)