Saggi Posted January 1, 2020 Share Posted January 1, 2020 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"; */ Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/ Share on other sites More sharing options...
requinix Posted January 1, 2020 Share Posted January 1, 2020 What is the problem? What code have you tried and what was it doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573025 Share on other sites More sharing options...
ginerjm Posted January 1, 2020 Share Posted January 1, 2020 If your input is a valid time string (or if not) there are plenty of date-related functions and formatting types that make your 'problem' a no-brainer. RTM! Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573033 Share on other sites More sharing options...
Barand Posted January 1, 2020 Share Posted January 1, 2020 For example, https://www.php.net/manual/en/datetime.createfromformat.php https://www.php.net/manual/en/datetime.format.php 2 Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573036 Share on other sites More sharing options...
Saggi Posted January 1, 2020 Author Share Posted January 1, 2020 6 hours ago, ginerjm said: If your input is a valid time string (or if not) there are plenty of date-related functions and formatting types that make your 'problem' a no-brainer. RTM! Though that's plausible but I try not to use date-related functions and formatting types. Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573041 Share on other sites More sharing options...
Saggi Posted January 1, 2020 Author Share Posted January 1, 2020 20 hours ago, Saggi said: 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"; */ function convertTimeFormat($time12Hour) { $time24Hour = ""; $Split = explode(":",$time12Hour); $Hour = $Split[0]; // $Minutes = $Split[1]; // $Seconds = $Split[2]; $Split[2] = substr($Split[2],0,2); if($Hour == '12' && strpos($time12Hour,"AM")!== FALSE) { } elseif(strpos($time12Hour,"PM")!== FALSE && $Hour != "12") { } elseif($Hour != "12" && strpos($time12Hour,"AM")!== FALSE) { echo $convertHour = 12+$Hour.":".$Split[1].":".$Split[2]."PM"; } return $time24Hour; } $time12Hour = "06:40:03AM"; $result = convertTimeFormat($time12Hour); Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573042 Share on other sites More sharing options...
Barand Posted January 1, 2020 Share Posted January 1, 2020 Why **ss about when there are inbuilt functions that can accomplish the task far more efficiently? function reformatTime($tstr) { $dt = DateTime::createFromFormat('h:i:sa', $tstr); return $dt->format('H:i:s'); } echo reformatTime('08:30:00PM'); //--> 20:30:00 Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573043 Share on other sites More sharing options...
ginerjm Posted January 1, 2020 Share Posted January 1, 2020 55 minutes ago, Saggi said: Though that's plausible but I try not to use date-related functions and formatting types. Fine. Fix the problem in your inimitable way then. Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573044 Share on other sites More sharing options...
gizmola Posted January 1, 2020 Share Posted January 1, 2020 1 hour ago, Saggi said: Though that's plausible but I try not to use date-related functions and formatting types. They are part of the language. Whenever possible you should always use the built in types and functions unless you have a very good reason not to. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573045 Share on other sites More sharing options...
Saggi Posted January 2, 2020 Author Share Posted January 2, 2020 9 hours ago, Barand said: Why **ss about when there are inbuilt functions that can accomplish the task far more efficiently? function reformatTime($tstr) { $dt = DateTime::createFromFormat('h:i:sa', $tstr); return $dt->format('H:i:s'); } echo reformatTime('08:30:00PM'); //--> 20:30:00 function convertTimeFormat($time12Hour){ echo date("H:i:s:A", strtotime("06:40:03 PM")); } $time12Hour="06:40:03PM"; echo $result=convertTimeFormat($time12Hour); Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573055 Share on other sites More sharing options...
Saggi Posted January 2, 2020 Author Share Posted January 2, 2020 8 hours ago, gizmola said: They are part of the language. Whenever possible you should always use the built in types and functions unless you have a very good reason not to. I want to learn PHP the hard way. lol. Anyway, I finished the task by both using in built and traditional way. Thanks for the input. Appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573056 Share on other sites More sharing options...
Saggi Posted January 2, 2020 Author Share Posted January 2, 2020 9 hours ago, ginerjm said: Fine. Fix the problem in your inimitable way then. I did. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573057 Share on other sites More sharing options...
gizmola Posted January 2, 2020 Share Posted January 2, 2020 A couple of comments on your code: function convertTimeFormat($time12Hour){ echo date("H:i:s:A", strtotime("06:40:03 PM")); } First off you are not using the parameter $time12hour. Some smart editors will introspect and alert you to this. You can also typehint your parameter and the return value. Since you require $time12Hour to be a string, it is helpful to add the typehint to the parameter. Also, with functions it is best practice to return a result. You shouldn't echo the result, but instead return it. You can also typehint the return value, which should also be a string. function convertTimeFormat(string $time12Hour): string { return date("H:i:s:A", strtotime($time12Hour)); } Now your example code becomes this: $time12Hour="06:40:03PM"; echo convertTimeFormat($time12Hour); You could also save the function result to a variable,. Quote Link to comment https://forums.phpfreaks.com/topic/309773-convert-a-string-from-12hour-time-format-to-24hour-format/#findComment-1573084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.