0, 'd' => 0, 'h' => 0, 'm' => 0, 's' => 0); if (!in_array($maxunit, array_keys($allowed), true)) throw new HumanReadableTimeException('illegal value for maxunit: "' . $maxunit . '"'); foreach ($allowed as $key => $value) { if ($maxunit == $key) break; unset($allowed[$key]); } $seconds = intval($seconds); $hrt = ''; foreach ($allowed as $key => $value) { switch ($key) { case 'w': $tmp = intval($seconds / (7*86400)); if ($tmp > 0) $seconds %= (7*86400); $allowed[$key] += $tmp; break; case 'd': $tmp = intval($seconds / (86400)); if ($tmp > 0) $seconds %= (86400); $allowed[$key] += $tmp; break; case 'h': $tmp = intval($seconds / (3600)); if ($tmp > 0) $seconds %= (3600); $allowed[$key] += $tmp; break; case 'm': $tmp = intval($seconds / (60)); if ($tmp > 0) $seconds %= (60); $allowed[$key] += $tmp; break; case 's': $allowed[$key] += $seconds; break; } } $hrt = ''; foreach ($allowed as $key => $value) { if ($value > 0) $hrt .= sprintf('%d%s', $value, $key); } return $hrt; } /** * parse a string of 3h2m7s and return the number of seconds as integer * add "s" to the end of the number if $addsecond is set to true * @param string $hr * @param boolean $addsecond * @return integer|string */ public static function HR2Seconds($hr, $addsecond = false) { $hr = trim($hr); if ($hr == '') { if ($addsecond === true) return '0s'; else return 0; } $hr = strtolower($hr); $matches = array(); if (preg_match_all('/([0-9]*)([wdhms])/', $hr, $matches, PREG_SET_ORDER) > 0) { $interval = 0; for ($i = 0; $i < count($matches); $i++) { switch ($matches[$i][2]) { case 'w': $interval += $matches[$i][1] * 7 * 86400; break; case 'd': $interval += $matches[$i][1] * 86400; break; case 'h': $interval += $matches[$i][1] * 3600; break; case 'm': $interval += $matches[$i][1] * 60; break; case 's': $interval += $matches[$i][1]; break; } } if ($addsecond === true) return sprintf('%ds', $interval); else return $interval; } if ($addsecond === true) return '0s'; else return 0; } }