Time Conversion Algorithm

Time Conversion, Algorithm

Given a time in -hour AM/PM format, convert it to military (-hour) time.

Note: Midnight is on a -hour clock, and on a -hour clock. Noon is on a -hour clock, and on a -hour clock.

Input Format

A single string containing a time in -hour clock format (i.e.: or ), where and .

Output Format

Convert and print the given time in -hour format, where .

Sample Input

07:05:45PM

Sample Output

19:05:45
<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%s",$time);

$time= explode(":", $time);
$pos = strpos(end($time), 'PM');

$h;
$m;
$s;

if ($pos === false) {
$h= ($time[0] == 12) ? '00' : $time[0];
$m= $time[1];
$t= intval(end($time));
$s= ($t<10) ? "0$t": $t;
}else{
$h= ($time[0] == 12) ? 12 : 12+$time[0];
$m= $time[1];
$t= intval(end($time));
$s= ($t<10) ? "0$t": $t;
}


echo $h.':'.$m.':'.$s;


?>

Leave a Reply

Your email address will not be published.