msaz87 Posted January 8, 2010 Share Posted January 8, 2010 Hey all, I made a form that lets the user input their time using three form fields: an hour, a minute and an AM/PM. They input it in a 12-hour clock style and then when it's submitted, it should be converted to a 24-hour style and added a DB. Below is the code I have for inputting the time and immediately after that is the conversion... but I've had troubles when it comes to times within 12 AM - 12:59 AM and 12 PM - 12:59 PM. Basically, entering anything between 12 PM - 12:59 PM doesn't show up and doing the same time frame for AM spits out the PM equivalent. So, just to explain once more.. if they entered 12 : 30 PM it would convert to 1230 and if they entered 12 : 30 AM it would convert to 0030. Any help is greatly appreciated -- thanks! <input type="text" size="3" name="add_time_hr"> : <input type="text" size="3" name="add_time_min"> <select name="add_am_pm"> <option>AM</option> <option>PM</option> </select> <input type="submit" value="add"> $add_am_pm = $_REQUEST['add_am_pm']; $add_time_hr = $_REQUEST['add_time_hr']; $add_time_min = $_REQUEST['add_time_min']; if($add_time_hr < 10) $add_time_hr = "0".$add_time_hr; switch($add_am_pm) { case "PM": if($add_time_hr == "12") $add_time_hr = 0; $add_time = $add_time_hr.$add_time_min + 1200; break; case "AM": $add_time = $add_time_hr.$add_time_min; break; } Link to comment https://forums.phpfreaks.com/topic/187685-help-converting-times/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 There are lots of ways of doing it, something like this should work... $hours = '12'; $minutes = '40'; $period = 'AM'; echo date("H:i", strtotime($hours.':'.$minutes.$period)); Link to comment https://forums.phpfreaks.com/topic/187685-help-converting-times/#findComment-990846 Share on other sites More sharing options...
msaz87 Posted January 8, 2010 Author Share Posted January 8, 2010 Perfect... thanks so much, Cags! Link to comment https://forums.phpfreaks.com/topic/187685-help-converting-times/#findComment-990852 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.