Jump to content

Help converting times


msaz87

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.