Hello All,
Need an advise in finishing this task please.
function convertTimeFormat($time12Hour) {
// Initialized required variable to an empty string.
$time24Hour = "";
// Used explode() function to break the string into an array and stored its value in $Split variable.
$Split = explode(":",$time12Hour); // print_r(explode (":", $time12Hour)); => Array ( [0] => 09 [1] => 50 [2] => 08AM )
// Retrieved only "hour" from the array and stored in $Hour variable.
$Hour = $Split[0];
$Split[2] = substr($Split[2],0,2);
// Used stripos() function to find the position of the first occurrence of a string inside another string.
if($Hour == '12' && strpos($time12Hour,"AM")!== FALSE) {
// Code here
} elseif(strpos($time12Hour,"PM")!== FALSE && $Hour != "12") {
// code here
}
return $time24Hour;
}
$time12Hour = "09:50:08AM";
$result = convertTimeFormat($time12Hour);
print_r($result);
/*
Input : "09:50:08AM";
Output : "21:50:08PM";
*/