Log in

View Full Version : AM/PM in online form



lindsaycb
11-18-2008, 09:51 PM
I have an online form that each 3 separate options for hour (dtHour), minute (dtMin) and am/pm (dtAP).

dtHour only goes 1-12. dtMin goes 00-55 in 5 increments. dtAP only has am & pm as options.

http://www.kishwaukeecollege.edu:16080/~lbarron/form.png

As each person submits their time, it goes into the database as the 24 hour time. But it shows up on the page as 12 hour format with am/pm.

However, when I choose 12, 00 and pm, the time shows up on the page as 5:59 pm. If I choose 12, 00 and am, the time shows up as 12:00 pm. The minute doesn't seem to be affected at all. It's just the hour.

http://www.kishwaukeecollege.edu:16080/~lbarron/time goes wrong.png

Below is the code that I am using in the form and in the PHP (both of them are PHP files).


print "<select name=\"dtAP\">\n";
print "<option value=\"0\">am</option>\n";
print "<option value=\"12\">pm</option>\n";
print "</select>\n";


$classTime = $_POST['dtHour'] + $_POST['dtAP'] . ":" . $_POST['dtMin'] . ":00";

I thought about just making the dtHour drop down option all 24 hour and just get rid of the am/pm but secretaries will be using this it deals with classes so I'd rather not mess with what they're used to; i.e. knowing the 12:30 pm class is cancelled. I'm sure I'll hear it if I do. They have a workaround by choosing 12, 30, and am to make a class be 12:30 pm but that's just confusing.

I'm not sure what else I need to provide for you to get a good grip on the code. It's just the 12 option in dtHour that seems to be messing up the am/pm options.

Any suggestions?

bluewalrus
11-18-2008, 11:22 PM
Maybe something like this, not sure though I'm no php master at all.


$find = ":";
$hour = strtok($dthour, $find);
$mins = strtok($find);
if ($hour > 12 ) {
$time="p.m";
$newtime = $hour - 12;
$dthour = $newtime . $mins . $time;
}
else {
$time = "a.m";
$checkit= $hour . $mins . $time;
}

lindsaycb
11-19-2008, 03:47 PM
Should I post this in the SQL forum then?

boogyman
11-19-2008, 09:03 PM
I generally work with timestamps when dealing with dates being stored in a database.

the php date() (http://php.net/date) function provides a very easy way to render the date in any format necessary

and php's strtotime() (http://php.net/strtotime) function can be used to create the timestamp from the values selected.



<?php

// put all of the values of the class into one string
$classDt = sprintf("%s %s %s %i:%i:%i %s", $month, $day, $year, (int)$hour, (int)$minute, (int)$second $meridiem);
// convert string to timestamp
strtotime($classDt);

?>