Re: [PHP-DB] Time input formatting in PHP-12 hour clock
With help from the List, I have had some success in answering my question about formatting 12-hr vs. 24-hr time and I thought I would share these two examples of what is working for me. The first example keeps the user from inputting invalid data. In the second, I note that an error yields a "-1" but the string produced appears to be null. --Dave Shugarts /* *** EXAMPLE I ** */ Hr: 01 02 03 04 05 06 07 08 09 10 11 12 Min: 0 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 AM/PM AM PM "; $time_as_timestamp=strtotime ($testtime); echo "Variable \"testtime_as_timestamp\" = $time_as_timestamp"; $time_as_string=strftime("%I:%M %p", $time_as_timestamp); echo "Variable \"testtime_as_string\" = $time_as_string"; ?> /* *** EXAMPLE II ** */ Hr: Min: AM/PM AM PM "; $time_as_timestamp2=strtotime ($testtime2); echo "Variable \"testtime_as_timestamp2\" = $time_as_timestamp2"; $time_as_string2=strftime("%I:%M %p", $time_as_timestamp2); echo "Variable \"testtime_as_string2\" = $time_as_string2"; ?> > > Hi, All-- > > Has anyone already solved the problem of how to get a time input from the > user in people time (12-hour clock) and format it to be INSERTed in a mySQL > time field? > > The mySQL time field is 24-hour and of course it can be formatted in the > SELECT statement to be displayed as 12-hour for the user. But--presumably in > PHP--I need the user to be able to input a time, then validate it as to > whether it is complete (e.g., expresses AM or PM and evaluates correctly), > then pass it into an INSERT statement into the time field of a database. > > I suspect this already exists, just can't find an example. > > TIA > > Dave Shugarts > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Time input formatting in PHP-12 hour clock
There are several ways you can do it. But the first step is to translate to 24 hour time. So the first thing we have to check is if the am or pm flag was set. Then add 12 hours or leave as is OR set the hour to 00 (midnight). Something like this may help: Switch(strtoupper($ampm_flag)) { // if it's am all we have to worry about is midnight because all hours before noon in 12 hour time //are the same as 24 except midnight. Case "AM": { If ($hour = 12) { $hour = 0; } break; } case "PM": { /* now we gotta check to see if the pm time is smaller that 12 if so add 12 hours if not leave it. Because the only thing that is the same in the afternoon between 12 and 24 hour time is 12 PM */ if ($hour < 12) { $hour += 12; } } /* now sprintf into mysql format. I'm more familiar with PERL's sprintf so check the PHP docs and make sure I got this right */ $mysqldate = sprintf ("%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $minute, $second); /*Then insert $mysqldate into your date field. You could also concatinate, just make sure that your digits are the right length, you could also check the mysql manual for the different formats of inserting datetimes. There are less friendly ways of inserting datetimes. */ /* by the way this was sort of done on the fly and was not tested. If anyone encounters any bugs, let me know */ Carl. -Original Message- From: David Shugarts [mailto:[EMAIL PROTECTED] Sent: Friday, June 13, 2003 10:27 AM To: [EMAIL PROTECTED] Subject: [PHP-DB] Time input formatting in PHP-12 hour clock Hi, All-- Has anyone already solved the problem of how to get a time input from the user in people time (12-hour clock) and format it to be INSERTed in a mySQL time field? The mySQL time field is 24-hour and of course it can be formatted in the SELECT statement to be displayed as 12-hour for the user. But--presumably in PHP--I need the user to be able to input a time, then validate it as to whether it is complete (e.g., expresses AM or PM and evaluates correctly), then pass it into an INSERT statement into the time field of a database. I suspect this already exists, just can't find an example. TIA Dave Shugarts -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Time input formatting in PHP-12 hour clock
Hi David Comments inline... On 13 Jun,2003 at 10:26 David Shugarts wrote: > PHP--I need the user to be able to input a time, then validate it as to > whether it is complete (e.g., expresses AM or PM and evaluates correctly), > then pass it into an INSERT statement into the time field of a database. > How about using a radio button for the AM / PM selection on the form? Then if its PM you can simply adjust the hour figure accordingly to convert to 24hr (add 12 or whatever) and then concatenate a string together for the INSERT, eg: HHMMSS. Validating is just a matter of making sure the numbers are sanea few if then's should do the trick. Alternatively use select dropdowns on the form to only allow a user to choose a valid time. Its up to them to get the RIGHT time, of course !!! If you go the validation route I think I would probably use a little javascript snippet to validate the numbers before the form is submitted. Personal preference really... Does this help at all? Ronan e: [EMAIL PROTECTED] t: 01903 739 997 w: www.thelittledot.com The Little Dot is a partnership of Ronan Chilvers and Giles Webberley -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php