Re: Time input formatting in PHP-12 hour clock

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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   ****** */


<html>

<form>
Hr: 
<SELECT NAME=hh SIZE=1>
<OPTION value=01> 01
<OPTION value=02> 02
<OPTION value=03> 03
<OPTION value=04> 04
<OPTION value=05> 05
<OPTION value=06> 06
<OPTION value=07> 07
<OPTION value=08> 08
<OPTION value=09> 09
<OPTION value=10> 10
<OPTION value=11> 11
<OPTION value=12> 12
</SELECT>
&nbsp; &nbsp;Min: 
<SELECT NAME=mm SIZE=1>
<OPTION value=00> 0
<OPTION value=01> 01
<OPTION value=02> 02
<OPTION value=03> 03
<OPTION value=04> 04
<OPTION value=05> 05
<OPTION value=06> 06
<OPTION value=07> 07
<OPTION value=08> 08
<OPTION value=09> 09
<OPTION value=10> 10
<OPTION value=11> 11
<OPTION value=12> 12
<OPTION value=13> 13
<OPTION value=14> 14
<OPTION value=15> 15
<OPTION value=16> 16
<OPTION value=17> 17
<OPTION value=18> 18
<OPTION value=19> 19
<OPTION value=20> 20
<OPTION value=21> 21
<OPTION value=22> 22
<OPTION value=23> 23
<OPTION value=24> 24
<OPTION value=25> 25
<OPTION value=26> 26
<OPTION value=27> 27
<OPTION value=28> 28
<OPTION value=29> 29
<OPTION value=30> 30
<OPTION value=31> 31
<OPTION value=32> 32
<OPTION value=33> 33
<OPTION value=34> 34
<OPTION value=35> 35
<OPTION value=36> 36
<OPTION value=37> 37
<OPTION value=38> 38
<OPTION value=39> 39
<OPTION value=40> 40
<OPTION value=41> 41
<OPTION value=42> 42
<OPTION value=43> 43
<OPTION value=44> 44
<OPTION value=45> 45
<OPTION value=46> 46
<OPTION value=47> 47
<OPTION value=48> 48
<OPTION value=49> 49
<OPTION value=50> 50
<OPTION value=51> 51
<OPTION value=52> 52
<OPTION value=53> 53
<OPTION value=54> 54
<OPTION value=55> 55
<OPTION value=56> 56
<OPTION value=57> 57
<OPTION value=58> 58
<OPTION value=59> 59
</SELECT>
&nbsp;&nbsp;AM/PM
<SELECT NAME=ampm SIZE=1>
<OPTION value="AM"> AM
<OPTION value="PM"> PM
</SELECT>
&nbsp;&nbsp;
<input type="submit">
</form> 



<?php

$testtime=$hh.":".$mm." ".$ampm;

echo "Testtime = $testtime<br><br>";


$time_as_timestamp=strtotime ($testtime);
echo "Variable \"testtime_as_timestamp\" = $time_as_timestamp<br><br>";
$time_as_string=strftime("%I:%M %p", $time_as_timestamp);
echo "Variable \"testtime_as_string\" = $time_as_string";

?>

</html>




/* ******* EXAMPLE II   ****** */


<html>

<form>
Hr:
<INPUT TYPE="TEXT" NAME="hh2" SIZE="6">
&nbsp; &nbsp;Min: 
<INPUT TYPE="TEXT" NAME="mm2" SIZE="6">
&nbsp;&nbsp;AM/PM
<SELECT NAME=ampm2 SIZE=1>
<OPTION value="AM"> AM
<OPTION value="PM"> PM
</SELECT>
&nbsp;&nbsp;
<input type="submit">
</form>

<?php

$testtime2=$hh2.":".$mm2." ".$ampm2;

echo "Testtime = $testtime2<br><br>";


$time_as_timestamp2=strtotime ($testtime2);
echo "Variable \"testtime_as_timestamp2\" = $time_as_timestamp2<br><br>";
$time_as_string2=strftime("%I:%M %p", $time_as_timestamp2);
echo "Variable \"testtime_as_string2\" = $time_as_string2";

?>

</html>



> 
> 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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux