FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » validating / converting time for insertion into mysql DB??
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
validating / converting time for insertion into mysql DB?? [message #172189] Thu, 03 February 2011 15:59 Go to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
I've got a form where users enter in time into a textbox.
txttime1
txttime2

usually for 12:15 the user enters 1215 without the :, so basically the
user does not enter :

and in the db the time goes into the db as: 00:12:15 instead of
12:15:00

So what I want to do is somehow fix the time so that when a user
enters it as 1215, it changes the time to 12:15

so than the $_POST[txttime1] would be correct at 12:15 and not 1215

How would this be done?
TIA
Re: validating / converting time for insertion into mysql DB?? [message #172190 is a reply to message #172189] Thu, 03 February 2011 17:32 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Feb 3, 3:59 pm, PAkerly <pake...@gmail.com> wrote:
> I've got a form where users enter in time into a textbox.
> txttime1
> txttime2
>
> usually for 12:15 the user enters 1215 without the :, so basically the
> user does not enter :
>
> and in the db the time goes into the db as: 00:12:15 instead of
> 12:15:00
>
> So what I want to do is somehow fix the time so that when a user
> enters it as 1215, it changes the time to 12:15
>
> so than the $_POST[txttime1] would be correct at 12:15 and not 1215
>
> How would this be done?
> TIA

Well first of all you need to decide on how you are going to interpret
what the user has entered.
was 120 supposed to be 01:20 or 12:00?

Suppose they enter 1h22, what do you plan to do then?

What you SHOULD do, is to validate that they have entered something in
a valid format and if not, send them an error message and ask them to
correct it.

Alternatively, split the boxes and have hours in one box and minutes
in another.
Re: validating / converting time for insertion into mysql DB?? [message #172191 is a reply to message #172189] Thu, 03 February 2011 17:57 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 03/02/11 15:59, PAkerly wrote:
> I've got a form where users enter in time into a textbox.
> txttime1
> txttime2
>
> usually for 12:15 the user enters 1215 without the :, so basically the
> user does not enter :
>
> and in the db the time goes into the db as: 00:12:15 instead of
> 12:15:00
>
> So what I want to do is somehow fix the time so that when a user
> enters it as 1215, it changes the time to 12:15
>
> so than the $_POST[txttime1] would be correct at 12:15 and not 1215
>
> How would this be done?
> TIA

The field is actually $_POST['txttime1']

You could do something like this:

<?php
function fixuphhmm($in) {
if preg_match("/^\d{2}:\d{2}$/",$in) return $in; // hh:mm
if preg_match("/^\d{1}:\d{2}$/",$in) return $in; // h:mm
if preg_match("/^\d{4}$/",$in) return
preg_replace("/^\(d{2})(\d{2})$/","$1:$2",$in); // hhmm
if preg_match("/^\d{3}$/",$in) return
preg_replace("/^\(d{1})(\d{2})$/","0$1:$2",$in); // hmm
return false; // unrecognised format
}

$tstr1 = fixuphhmm($_POST['txttime1']);
$tstr2 = fixuphhmm($_POST['txttime2']);
if (!$tstr1 || !$tstr2) {
// do something about it
}
?>

And you might also want to consider the following field formats:

h/hh:m/mm:s/ss
hmmss
hhmmss
mmss
mss

The last two give you problems. How do you tell whether 3 or 4 digits is
hours and minutes or minutes and seconds?

The real problem is that you're allowing your user to give you ambiguous
input and then trying to make sense of it.

Use more form fields: t1hours t1mins and t2hours t2mins

You should also consider whether you need an am / pm indication in case
someone puts 2h23m when they mean 14:23

So instead of txttime1, perhaps:

<input type='text' size='2' maxlength='2' name='time1hours' title='Hours
0 .. 23'>:<input type='text' size='2' maxlength='2' name='time1mins'
title='Minutes 0 .. 59'>

and for txttime2:

<input type='text' size='2' maxlength='2' name='time2hours' title='Hours
0 .. 23'>:<input type='text' size='2' maxlength='2' name='time2mins'
title='Minutes 0 .. 59'>

You could use select lists for hours and minutes, although 60 element
select lists are "too long" for some people's liking.

Rgds

Denis McMahon
Re: validating / converting time for insertion into mysql DB?? [message #172213 is a reply to message #172191] Fri, 04 February 2011 15:37 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Denis McMahon)

> […]
> And you might also want to consider the following field formats:
>
> h/hh:m/mm:s/ss
> hmmss
> hhmmss
> mmss
> mss
>
> The last two give you problems. How do you tell whether 3 or 4 digits is
> hours and minutes or minutes and seconds?
>
> The real problem is that you're allowing your user to give you ambiguous
> input and then trying to make sense of it.
>
> Use more form fields: t1hours t1mins and t2hours t2mins

While this solves the ambiguity problem, it creates a usability problem
and is a pain to use. Even more ugly would be dropdown lists …

The best method for the user is a single field, and the developer should
take care of the correct parsing. You could also enforce a particular
format and reject all other ones (of course then there should be a short
note or an example near the input field how to enter the time).

> You could use select lists for hours and minutes

Arrrgh … ;-)

> although 60 element
> select lists are "too long" for some people's liking.

People who do this also eat little children.

Micha
Re: validating / converting time for insertion into mysql DB?? [message #172216 is a reply to message #172213] Fri, 04 February 2011 18:00 Go to previous messageGo to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
On Feb 4, 10:37 am, Michael Fesser <neti...@gmx.de> wrote:
> .oO(Denis McMahon)
>
>
>
>> […]
>> And you might also want to consider the following field formats:
>
>> h/hh:m/mm:s/ss
>> hmmss
>> hhmmss
>> mmss
>> mss
>
>> The last two give you problems. How do you tell whether 3 or 4 digits is
>> hours and minutes or minutes and seconds?
>
>> The real problem is that you're allowing your user to give you ambiguous
>> input and then trying to make sense of it.
>
>> Use more form fields: t1hours t1mins and t2hours t2mins
>
> While this solves the ambiguity problem, it creates a usability problem
> and is a pain to use. Even more ugly would be dropdown lists …
>
> The best method for the user is a single field, and the developer should
> take care of the correct parsing. You could also enforce a particular
> format and reject all other ones (of course then there should be a short
> note or an example near the input field how to enter the time).
>
>> You could use select lists for hours and minutes
>
> Arrrgh … ;-)
>
>> although 60 element
>> select lists are "too long" for some people's liking.
>
> People who do this also eat little children.
>
> Micha


Here is how I display the data on the screen so that even if they
enter 1215 it shows 12:15
.....
<?php echo time_format($_POST[txttime1])?>

Can I also somehow use this to get it into the db this way?
Re: validating / converting time for insertion into mysql DB?? [message #172217 is a reply to message #172216] Fri, 04 February 2011 18:02 Go to previous messageGo to next message
PAkerly is currently offline  PAkerly
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
On Feb 4, 1:00 pm, PAkerly <pake...@gmail.com> wrote:
> On Feb 4, 10:37 am, Michael Fesser <neti...@gmx.de> wrote:
>
>
>
>> .oO(Denis McMahon)
>
>>> […]
>>> And you might also want to consider the following field formats:
>
>>> h/hh:m/mm:s/ss
>>> hmmss
>>> hhmmss
>>> mmss
>>> mss
>
>>> The last two give you problems. How do you tell whether 3 or 4 digits is
>>> hours and minutes or minutes and seconds?
>
>>> The real problem is that you're allowing your user to give you ambiguous
>>> input and then trying to make sense of it.
>
>>> Use more form fields: t1hours t1mins and t2hours t2mins
>
>> While this solves the ambiguity problem, it creates a usability problem
>> and is a pain to use. Even more ugly would be dropdown lists …
>
>> The best method for the user is a single field, and the developer should
>> take care of the correct parsing. You could also enforce a particular
>> format and reject all other ones (of course then there should be a short
>> note or an example near the input field how to enter the time).
>
>>> You could use select lists for hours and minutes
>
>> Arrrgh … ;-)
>
>>> although 60 element
>>> select lists are "too long" for some people's liking.
>
>> People who do this also eat little children.
>
>> Micha
>
> Here is how I display the data on the screen so that even if they
> enter 1215 it shows 12:15
> ....
> <?php echo time_format($_POST[txttime1])?>
>
> Can I also somehow use this to get it into the db this way?

Sorry, forgot to include function...

function time_format($string){
if(preg_match('/^(\d{1,2})([0-5][0-9])$/', $string, $matches))
{
if($matches[1]>=0 && $matches[1]<=23){
return $matches[1] . ':' . $matches[2];
}
}

return $string;

}
Re: validating / converting time for insertion into mysql DB?? [message #172221 is a reply to message #172189] Sat, 05 February 2011 07:53 Go to previous messageGo to next message
Jim Higgins is currently offline  Jim Higgins
Messages: 20
Registered: November 2010
Karma: 0
Junior Member
On Thu, 3 Feb 2011 07:59:38 -0800 (PST), PAkerly <pakerly(at)gmail(dot)com>
wrote:

> I've got a form where users enter in time into a textbox.
> txttime1
> txttime2
>
> usually for 12:15 the user enters 1215 without the :, so basically the
> user does not enter :
>
> and in the db the time goes into the db as: 00:12:15 instead of
> 12:15:00
>
> So what I want to do is somehow fix the time so that when a user
> enters it as 1215, it changes the time to 12:15
>
> so than the $_POST[txttime1] would be correct at 12:15 and not 1215
>
> How would this be done?
> TIA


What does this time your users are entering actually represent? If
it's what time they woke up that day then I guess they'll have to
enter it and you'll need to validate the data before accepting it, but
if it's the time the form was submitted, why leave that to the user to
enter? Just insert the time in your form handler.

If that approach meets your needs, then define...

$the_time = date("H:i:s",time());

in your PHP script then let the form handler insert $the_time into
your MySQL table along with the rest of the form data.
Re: validating / converting time for insertion into mysql DB?? [message #172222 is a reply to message #172189] Sat, 05 February 2011 08:11 Go to previous message
P E Schoen is currently offline  P E Schoen
Messages: 86
Registered: January 2011
Karma: 0
Member
"PAkerly" wrote in message
news:cc56a3eb-87fe-4d5f-bd8e-23c19238abfa(at)g1g2000prb(dot)googlegroups(dot)com...

> I've got a form where users enter in time into a textbox.
> txttime1
> txttime2

You could use a date/time picker where the user selects a date from a
calendar and the time can be adjusted with spin controls. See an example at:
http://www.pauleschoen.com/SCGBG/EventSubmit.htm

There is a text box where the user can also directly edit it but I use a
simple Javascript format checker in case they screw up:

else if ( (Form1.Start_DT.value.charAt(2) != "/") ||
(Form1.Start_DT.value.charAt(5) != "/") ||
(Form1.Start_DT.value.charAt(10) != " ") ||
(Form1.Start_DT.value.charAt(13) != ":") )
{
alert ("Please use mm/dd/yyyy hh:mm Format for Date/Time");
Form1.Start_DT.focus();
}
else { //Everything seems OK
with (Form1.Start_DT) { //Update hidden variable to be sent
Form1.Entry_Start_DT.value =
value.substring(6,10) + "-" +
value.substring(0,2) + "-" +
value.substring(3,5) + "T" +
value.substring(11,13) + ":" +
value.substring(14,16);
}

Not the best, but it works. Note that I format the date in standard format:
yyyymmddThh:mm

I might use some of the other ideas submitted here.

Thanks,

Paul
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: pass variable in php construct
Next Topic: Looping through Web Service results
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 07:16:56 GMT 2024

Total time taken to generate the page: 0.02288 seconds