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
Return to the default flat view Create a new topic Submit Reply
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 previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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 11:32:56 GMT 2024

Total time taken to generate the page: 0.04887 seconds