Forums

PHP

This topic is locked

12 hour time to MySQL

Posted 06 Jan 2005 23:26:39
1
has voted
06 Jan 2005 23:26:39 Walter Deinzer posted:
I'm stumped on something that seems basic. I have a form that has a field that calls for a time. Most people are used to using a 12 hour time format. If I send 3:00 PM to my database it ends up 3:00 AM. Any thoughts on how to fix the problem? Can I do it in the way the database is set up or do I need to make some sort of conversion before it is posted. By the way if I send 15:00 to the database it ends up 3:00 PM as I want.

Thanks for you your help, Walter

Replies

Replied 07 Jan 2005 01:23:47
07 Jan 2005 01:23:47 Chris Charlton replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>...I have a form that has a field that calls for a time. Most people are used to using a 12 hour time format. If I send 3:00 PM to my database it ends up 3:00 AM. Any thoughts on how to fix the problem? Can I do it in the way the database is set up or do I need to make some sort of conversion before it is posted. By the way if I send 15:00 to the database it ends up 3:00 PM as I want.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

HTML/PHP way: If you have the select menu/lists that display different times, then you could just alter the values to be 24-hr times.

PHP/MySQL way: alter the SQL code (INSERT & UPDATE statements) to reformat the value that's going into the table. Check out PHP's Date/Time functions: www.php.net/manual/en/ref.datetime.php

Oh, and Server Formats might help too, here's an article <i>FREE! Dreamweaver/Ultradev - Server (Binding) Formats</i> - www.dmxzone.com/go?7613

~~~~~~~~~~~~~~~
Chris Charlton

Sr. Developer - www.fingerprintMedia.com
Replied 07 Jan 2005 17:59:09
07 Jan 2005 17:59:09 Walter Deinzer replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>...I have a form that has a field that calls for a time. Most people are used to using a 12 hour time format. If I send 3:00 PM to my database it ends up 3:00 AM. Any thoughts on how to fix the problem? Can I do it in the way the database is set up or do I need to make some sort of conversion before it is posted. By the way if I send 15:00 to the database it ends up 3:00 PM as I want.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>



PHP/MySQL way: alter the SQL code (INSERT & UPDATE statements) to reformat the value that's going into the table. Check out PHP's Date/Time functions: www.php.net/manual/en/ref.datetime.php

~~~~~~~~~~~~~~~
Chris Charlton

Sr. Developer - www.fingerprintMedia.com
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Chris, thank you for the suggestions. The one above is of most interest to me: Reformatting what goes into the table, but I confess I'm a bit stumped on how to do that. Do you have any further suggestions?

Walter
Replied 07 Jan 2005 22:36:15
07 Jan 2005 22:36:15 Chris Charlton replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>...PHP/MySQL way: alter the SQL code (INSERT & UPDATE statements) to reformat the value that's going into the table. Check out PHP's Date/Time functions: www.php.net/manual/en/ref.datetime.php ...
--------------
Chris, thank you for the suggestions. The one above is of most interest to me: Reformatting what goes into the table, but I confess I'm a bit stumped on how to do that. Do you have any further suggestions?<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Look at the INSERT/UPDATE code that DW writes, you'll see some lines start with "Get_SQL_Value" or something like that, anyway, you'll see is have the formfield name/id, you can make an edit there.

Sorry, I don't have the right function name off hand, but my laptop just dies this morning! <img src=../images/dmxzone/forum/icon_smile_sad.gif border=0 align=middle>
Replied 12 Jan 2005 19:04:11
12 Jan 2005 19:04:11 Walter Deinzer replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
[Look at the INSERT/UPDATE code that DW writes, you'll see some lines start with "Get_SQL_Value" or something like that, anyway, you'll see is have the formfield name/id, you can make an edit there.

Sorry, I don't have the right function name off hand, but my laptop just dies this morning! <img src=../images/dmxzone/forum/icon_smile_sad.gif border=0 align=middle>
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Chris, thank you. That worked. I will supply some code below that I used for testing. I did not use DM to make the database connection in this code, but it worked. The other thing I did was to change the field in
MySQL to a bigint field instead of a time field. That was at the suggestion of a posting I read on another forum. Here is the code. (I've **** out the actual server, database and password information. Also I've used a field of TimeTest and divided it into hours and minutes and then used mktime to add 12 hours. Interestingly, if 12 was used as the hour, I had to convert it to 0 in order to get the right time.)

&lt;?php
$AMPM=$_POST['AMPM'];
$TestTimeH=$_POST['TestTimeH'];
$TestTimeM=$_POST['TestTimeM'];
if ($TestTimeH==12) {
$TestTimeH=0;
}
?&gt;
&lt;?php
mysql_connect("******","root","*****" or die ("Unable to connect to MySQL server.";
$db = mysql_select_db("******" or die ("Unable to select requested database.";
//Assign contents of form to variables
if ($AMPM=="AM" {
$SendTime=mktime($TestTimeH,$TestTimeM,0,date("m",date("d",date("Y");
} else {
$SendTime=mktime($TestTimeH+12,$TestTimeM,0,date("m",date("d",date("Y");
}
$sql="INSERT INTO tbltimetest SET TimeTest='$SendTime'";
mysql_query ($sql);
echo mysql_error();
mysql_close();
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Date Test&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="DateTest.php" method="post" name="formtest" id="formtest"&gt;
&lt;input name="TestTimeH" type="text" id="TestTimeH" size="2" /&gt;
:
&lt;input name="TestTimeM" type="text" id="TestTimeM" size="2"&gt;
&lt;select name="AMPM" id="AMPM"&gt;
&lt;option value="AM"&gt;AM&lt;/option&gt;
&lt;option value="PM"&gt;PM&lt;/option&gt;
&lt;/select&gt;
&lt;br /&gt;
&lt;input type="submit" value="Store in database" /&gt;&lt;input type="reset" value="Reset fields" /&gt;
&lt;/form&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/body&gt;
&lt;/html&gt;

Reply to this topic