Results 1 to 4 of 4

Thread: AM/PM in online form

  1. #1
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default AM/PM in online form

    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.



    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.



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

    PHP Code:
    print "<select name=\"dtAP\">\n";
    print 
    "<option value=\"0\">am</option>\n";
    print 
    "<option value=\"12\">pm</option>\n";
    print 
    "</select>\n"
    PHP Code:
    $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?

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Maybe something like this, not sure though I'm no php master at all.

    Code:
    $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;
    }

  3. #3
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Should I post this in the SQL forum then?

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    I generally work with timestamps when dealing with dates being stored in a database.

    the php date() function provides a very easy way to render the date in any format necessary

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

    PHP Code:
    <?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);

    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •