3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function f($num) { if($num < 20) { switch($num) { //case 0: return 'zero'; break; case 1: return 'one'; break; case 2: return 'two'; break; case 3: return 'three'; break; case 4: return 'four'; break; case 5: return 'five'; break; case 6: return 'six'; break; case 7: return 'seven'; break; case 8: return 'eight'; break; case 9: return 'nine'; break; case 10: return 'ten'; break; case 11: return 'eleven'; break; case 12: return 'twelve'; break; case 13: return 'thirteen'; break; case 15: return 'fifteen'; break; case 18: return 'eighteen'; break; default: //14, 16, 17, 19 return f($num-10).'teen'; break; } } var_dump($num); $buf = (int)floor($num/10); var_dump($buf); $fraction = $num - $buf; switch($buf) { case 2: $str = 'twenty'; break; case 3: $str = 'thirty'; break; case 5: $str = 'fifty'; break; case 8: $str = 'eighty'; break; default: //4, 6, 7, 9 $str = f($buf).'ty'; break; } if($fraction > 0) { $str .= ' '.f($fraction); } return $str; } function timeInWords($h, $m) { $hstr = f($h); if($m == 0) { return $hstr." o' clock"; } if($m == 30) { return 'half past '.$hstr; } if($m < 30) { if($m == 15) { return 'quarter past '.$hstr; } return f($m).' minute'.($m > 0 ? 's': '').' past '.$hstr; } else { if($m == 45) { return 'quarter to '.f($h+1); } $mtmp = 60 - $m; return f($mtmp).' minute'.($m > 0 ? 's': '').' to '.f($h+1); } } echo timeInWords(6, 35);
based on KS1q4
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.3.0rc3
int(25) int(2) int(23) int(2) int(21) int(2) twenty twenty twenty nineteen minutes to seven