function seconds2times($seconds, $count_zero = TRUE, $output = 'years,days,hours,minutes,seconds')
{
// Normalize output
$output = trim(strtolower((string)$output));
if (!$output) {
// Invalid output
return FALSE;
}
// Array with the output formats
$output = preg_split('/[^a-z]+/', $output);
// Convert the list of outputs to an associative array
$output = array_combine($output, array_fill(0, count($output), 0));
// Make the output values into keys
extract(array_flip($output), EXTR_SKIP);
$times = [];
// Seconds per minute | hour | day | year
$periods = [60, 3600, 86400, 31556926,];
for ($i = 3; $i >= 0; $i--) {
$period = floor($seconds / $periods[$i]);
if (($period > 0) || ($period == 0 && $count_zero)) {
$times[$i + 1] = $period;
$seconds -= $period * $periods[$i];
$count_zero = TRUE;
}
}
$times[0] = $seconds;
$result = [];
if (isset($output['years'])) {
$result['years'] = $times[4];
}
if (isset($output['days'])) {
$result['days'] = $times[3];
}
if (isset($output['hours'])) {
$result['hours'] = $times[2];
}
if (isset($output['minutes'])) {
$result['minutes'] = $times[1];
}
if (isset($output['seconds'])) {
$result['seconds'] = $times[0];
}
return $result;
}
$time = '2d 9h 40m';
if (preg_match('/([\d]*)d\s([\d]*)h\s([\d]*)m/', $time, $matches)) {
list($time, $days, $hours, $minutes) = $matches;
$seconds = ($days * 86400) + ($hours * 3600) + ($minutes * 60);
$seconds = $seconds - ($seconds / 100 * 20); // -20%
print_r(seconds2times($seconds));
// Array ( [years] => 0 [days] => 1 [hours] => 22 [minutes] => 8 [seconds] => 0 )
}