Skip to content

Instantly share code, notes, and snippets.

@jovialcore
Created May 28, 2023 15:28
Show Gist options
  • Save jovialcore/ae5fe8a16e4c718d6c3609acd83d6a70 to your computer and use it in GitHub Desktop.
Save jovialcore/ae5fe8a16e4c718d6c3609acd83d6a70 to your computer and use it in GitHub Desktop.
time conversion in 24 hrs hacker rank test
<?php
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function timeConversion($s) {
$arr = explode(':', $s);
$timeoftheday = strtolower(substr($s, 8));
if ($timeoftheday == 'pm') {
if ($arr[0] == '12') {
$newStr = str_replace(substr($arr[2], 2), '', $arr[2]);
$arr[2] = $newStr;
return implode(':', $arr);
}
$r = $arr[0] + 12;
$arr[0] = (string) $r;
$newStr = str_replace(substr($arr[2], 2), '', $arr[2]);
$arr[2] = $newStr;
return implode(':', $arr);
} elseif ($timeoftheday == 'am') {
if ($arr[0] == '12') {
$arr[0] = '00';
}
$newStr = str_replace(substr($arr[2], 2), '', $arr[2]);
$arr[2] = $newStr;
return implode(':', $arr);
}
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$s = rtrim(fgets(STDIN), "\r\n");
$result = timeConversion($s);
fwrite($fptr, $result . "\n");
fclose($fptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment