ClarkeRubber

UNSW ProgComp: Problem 1 - 2007

Jun 9th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. 4
  5. 0509
  6. 1685
  7. 2933
  8. 2259
  9. END;
  10.  
  11. $input = explode("\n", $input);
  12. array_shift($input); //scrap the first line
  13.  
  14. foreach($input as $line){
  15.     $line2 = str_split($line);
  16.     $hour = $line2[0].$line2[1];
  17.     $minute = $line2[2].$line2[3];
  18.     $break = false;
  19.     if($minute > 59 || $hour > 23){
  20.         echo str_pad($line, 7)."Invalid time\n";
  21.         $break = true;
  22.     }
  23.     if($hour > 11){
  24.         $append = ' PM';
  25.     }else{
  26.         $append = ' AM';
  27.     }
  28.     if($hour > 12){
  29.         $hour -= 12;
  30.     }
  31.     if($break == false){
  32.         echo str_pad($line, 7).str_pad(intval($hour), 2, ' ', STR_PAD_LEFT).':'.$minute.$append."\n";
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment