echo timeInWords(5, 45)."\n";
echo timeInWords(3, 00)."\n";
echo timeInWords(7, 15)."\n";
echo timeInWords(6, 30)."\n";
echo timeInWords(2, 50)."\n";
echo timeInWords(8, 01)."\n";
echo timeInWords(9, 59)."\n";
//Using else if function
function timeInWords($h, $m) {
$res = (" ");
if ($m == 00) {
$res = $h . " o'clock";
} else if ($m == 30) {
$res = "Half past " . $h;
} else if ($m == 15) {
$res = "Quarter past " . $h;
} else if ($m == 45) {
$res = "Quarter to " . $h + 1;
} else if ($m < 30) {
$res = $m . " minutes past " . $h;
} else {
$res = 60 - $m . " minutes to " . $h + 1;
}
return $res;
}
//Using switch function
function timeInWords_2($h, $m) {
$res = (' ');
switch ($m) {
case '00';
$res = $h . " o'clock";
break;
case '30';
$res = "Half past " . $h;
break;
case '45';
$res = "Quarter to " . $h + 1;
break;
case '15';
$res = "Quarter past " . $h;
break;
case $m < 30;
$res = $m . " minutes past " . $h;
break;
case $m > 30;
$res = 60 - $m . " minutes to " . $h + 1;
break;
}
return $res;
}
//Both work, second solution is mine