Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
$inputs = [
"5d 3h 10m",
"8m",
"8d 19h 2m",
"5d 3h 10m",
"6h 11m",
];
function convert_to_seconds($input) {
$secs = [
"d" => 24*60*60,
"h" => 60*60,
"m" => 60,
];
$parts = explode(" ", $input);
$time = 0;
foreach ($parts as $part) {
foreach ($secs as $key => $value) {
if (strpos($part, $key)) {
$time += str_replace("", $key, $part) * $value;
}
}
}
return $time;
}
function plur($time){
$time = abs($time);
$t1 = $time % 10;
$t2 = $time % 100;
return ($t1 == 1 && $t2 != 11 ? "секунда" : ($t1 >= 2 && $t1 <= 4 && ($t2 < 10 || $t2 >= 20) ? "секунды" : "секунд"));
}
foreach ($inputs as $input) {
echo plur(convert_to_seconds($input)), "\n";
}
?>