darstjeff Posted May 6, 2012 Share Posted May 6, 2012 Hello, I am new to PHP> I have been doing a directive study program in college to learn some basic PHP. My instructor has given me some simple problems the code below is to Write a PHP program which will accept parameters indicated hour, minutes, second, and am or pm and convert that number to military time. I have the am working, but the elseif does not want run when I select pm in my form. I need some help in getting this to work, even if I am way off base in how I am working on this. Thank you <form action="<?php echo(htmlentities($_server['PHP_SELF']));?>" method="GET" > <input name="hours" type="text" id="txthours" value="<?php echo $hour; ?>" size="10" maxlength="2"> <input name="minutes" type="text" id="txtminutes" value="<?php echo $minute; ?>" size="10" maxlength="2"> <input name="seconds" type="text" id="txtseconds" value="<?php echo $second; ?>" size="10" maxlength="2"> <select name="select" id="select"> <option value="am">AM</option> <option value="pm">PM</option> </select> <br /> <br /> <input name="Submit" type="submit" value="Submit"> </p> </form> <?php $hour = htmlentities($_GET["hours"]); $minute = htmlentities($_GET["minutes"]); $second = htmlentities($_GET["seconds"]); $ampm = htmlentities($_GET["select"]); $lenhour = strlen($hour); if ($_GET["Submit"] == "Submit"){ /*Validating the text box to make sure that there are two digits*/ $lenhour = strlen($hour); $lenminute = strlen($minute); $lensecond = strlen($second); if(preg_match("/^[0-24]/", $hour)) { if ($lenhour != 2 and $hour != NULL){ $hour = ("0". $hour); } } if(preg_match("/^[0-24]/", $minute)) { if ($lenminute != 2 and $minute != NULL){ $minute = ("0" . $minute); } } if(preg_match("/^[0-24]/", $second)) { if ($lensecond != 2 and $second != NULL){ $second = ("0" . $second); } } //echo $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; if ($ampm = "am"){ if ($hour <= 12){ $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; print( date("H:i:s", strtotime($event_start))); } else { echo ("The numbers have to be lessa than or equal to 12"); } } elseif ($ampm = "pm"){ if ($hour <= 23){ echo $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; print( date("H:i:s", strtotime($event_start))); } else { echo ("make sure that you have selected the pm for the ranges of numbers you have chosen"); } } else { echo ("Please make sure you have enter a number value in each feild"); } } Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 6, 2012 Share Posted May 6, 2012 you need to use the double == for checking the values if ($ampm == "am"){ elseif ($ampm == "pm"){ Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted May 6, 2012 Share Posted May 6, 2012 you need to use the double == for checking the values if ($ampm == "am"){ elseif ($ampm == "pm"){ http://php.net/manual/en/language.operators.comparison.php Quote Link to comment Share on other sites More sharing options...
darstjeff Posted May 6, 2012 Author Share Posted May 6, 2012 Thank you, that worked. If anyone has any better idea on how I could write the code, I am all ears. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 6, 2012 Share Posted May 6, 2012 After your am pm check, you also check if the am is <=12, and if pm <=23. if any of the hours are >12 it's already military time. There's no such thing as 13:00 pm. It's either 1 pm or 13:00. So that part seems wrong to me. You also have an error message within your PM check that will never occur and doesn't make any sense. If they chose PM, and the hour is > 23, you tell them to select PM for the hour range. Not sure what you're trying to do there. You also have multiple spelling errors. Quote Link to comment Share on other sites More sharing options...
darstjeff Posted May 6, 2012 Author Share Posted May 6, 2012 I was using the <=12, and if pm <=23 to validate the input. thank you Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted May 7, 2012 Share Posted May 7, 2012 maybe someone can back me up on this but it seems when i replaced if($hour => 23) with if(strlen($hour) => 23) it reduced so many php lint errors. also using the (string) typecast in front of all the $_GET variables seemed to reduce more warnings and errors. there were some minor errors i don't even understand and they may not be that big of a deal. just a bunch of the echo's it deemed were "not assigned". although i am sure you defined it somewhere else in the code. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 7, 2012 Share Posted May 7, 2012 it should be >= not => Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted May 7, 2012 Share Posted May 7, 2012 actually in the code it is <= either way it doesnt recognize it as an error. however i think his regex may be off it is telling me it should be a boolean instead of an int. revised code returns 7 errors. <form action="<?php echo(htmlentities($_server['PHP_SELF']));?>" method="GET" > <input name="hours" type="text" id="txthours" value="<?php echo $hour; ?>" size="10" maxlength="2"> <input name="minutes" type="text" id="txtminutes" value="<?php echo $minute; ?>" size="10" maxlength="2"> <input name="seconds" type="text" id="txtseconds" value="<?php echo $second; ?>" size="10" maxlength="2"> <select name="select" id="select"> <option value="am">AM</option> <option value="pm">PM</option> </select> <br /> <br /> <input name="Submit" type="submit" value="Submit"> </p> </form> <?php $hour = (string) $_GET["hours"] . htmlentities($hour); $minute = (string) $_GET["minutes"] . htmlentities($minute); $second = (string) $_GET["seconds"] . htmlentities($second); $ampm = (string) $_GET["select"] . htmlentities($ampm); $lenhour = strlen($hour); if ($_GET["Submit"] === "Submit"){ /*Validating the text box to make sure that there are two digits*/ $lenhour = strlen($hour); $lenminute = strlen($minute); $lensecond = strlen($second); if(preg_match("#^[0-24]/", $hour)) { if ($lenhour !== 2 and $hour !== NULL){ $hour = ("0". $hour); } } if(preg_match("#^[0-24]/", $minute)) { if ($lenminute !== 2 and $minute !== NULL){ $minute = ("0" . $minute); } } if(preg_match("#^[0-24]/", $second)) { if ($lensecond !== 2 and $second !== NULL){ $second = ("0" . $second); } } //echo $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; if ($ampm === "am"){ if (strlen($hour) <= 12){ $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; print( date("H:i:s", strtotime($event_start))); } else { echo ("The numbers have to be lessa than or equal to 12"); } } elseif ($ampm === "pm"){ if (strlen($hour) <= 23){ echo $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; print( date("H:i:s", strtotime($event_start))); } else { echo ("make sure that you have selected the pm for the ranges of numbers you have chosen"); } } else { echo ("Please make sure you have enter a number value in each feild"); } } Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted May 7, 2012 Share Posted May 7, 2012 Revised to 4 errors: <form action="<?php echo(htmlentities($_server['PHP_SELF']));?>" method="GET" > <input name="hours" type="text" id="txthours" value="<?php echo $hour; ?>" size="10" maxlength="2"> <input name="minutes" type="text" id="txtminutes" value="<?php echo $minute; ?>" size="10" maxlength="2"> <input name="seconds" type="text" id="txtseconds" value="<?php echo $second; ?>" size="10" maxlength="2"> <select name="select" id="select"> <option value="am">AM</option> <option value="pm">PM</option> </select> <br /> <br /> <input name="Submit" type="submit" value="Submit"> </p> </form> <?php $hour = (string) $_GET["hours"] . htmlentities($hour); $minute = (string) $_GET["minutes"] . htmlentities($minute); $second = (string) $_GET["seconds"] . htmlentities($second); $ampm = (string) $_GET["select"] . htmlentities($ampm); $lenhour = strlen($hour); if ($_GET["Submit"] === "Submit"){ /*Validating the text box to make sure that there are two digits*/ $lenhour = strlen($hour); $lenminute = strlen($minute); $lensecond = strlen($second); if(preg_match("/^[0-24]/", $hour) > 0) { if ($lenhour !== 2 and $hour !== NULL){ $hour = ("0". $hour); } } if(preg_match("/^[0-24]/", $minute) > 0) { if ($lenminute !== 2 and $minute !== NULL){ $minute = ("0" . $minute); } } if(preg_match("/^[0-24]/", $second) > 0) { if ($lensecond !== 2 and $second !== NULL){ $second = ("0" . $second); } } //echo $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; if ($ampm === "am"){ if (strlen($hour) <= 12){ $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; print( date("H:i:s", strtotime($event_start))); } else { echo ("The numbers have to be lessa than or equal to 12"); } } elseif ($ampm === "pm"){ if (strlen($hour) <= 23){ echo $event_start = $hour . ':' . $minute . ':' . $second . ' ' . $ampm; print( date("H:i:s", strtotime($event_start))); } else { echo ("make sure that you have selected the pm for the ranges of numbers you have chosen"); } } else { echo ("Please make sure you have enter a number value in each feild"); } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 It would help if you tell what the errors are. strlen gives you the length of the string. If you're letting them pick between 1 and 23 (which again, is already military time but you've chosen to ignore that I guess) strlen($hour) will never be greater than 2. The strlen of '1463' is 4. Logically what you're doing doesn't make sense. You can make the whole thing a lot shorter. Quote Link to comment 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.