Verifying time in php [message #179375] |
Sun, 14 October 2012 09:45  |
houghi
Messages: 45 Registered: September 2011
Karma: 0
|
Member |
|
|
I have a form that has the following entry fields:
hours
minutes
seconds
I need the time in seconds and that works:
$realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds'];
However people can fill out nothing or they could enter characters
What is the easiest way to verify if what the people filled out is
correct? I have tried, but I get lost in the if statements.
Extra difficult is that they must not fill out all three. Only one is
enough. e.g.
1 hour and no minutes or second gives 3600
1 hour, 1 minute and 1 second gives 3661
1 hour and 'X' should give an error.
All empty should give an error
empty hour 70 minutes and 75 seconds gives 4275.
Hours of more then 24 will be possible as well.
I might even add number of days.
If the last one gives an error, but it makes the life much easier, I can
live with that
It is the fact that they do not need to fill out all three that I am
stuck.
What I have tried is first to see if all are empty
Next try to see if each one is not a number
Then I need to figure out if there is a combination of some of it.
And that is when I get lost in the if statements and loops in loops.
I am sure there is an easy way to tell if all is nice and works well,
but how? Seaching google gave me transfering from seconds to H:i:s, but
that is not what I want.
houghi
--
This is written under the inluence of the following:
> Artist : Jeff Wayne
> Song : Horsell Common And The Heat Ray
> Album : The War Of The Worlds (Disc 1)
|
|
|
Re: Verifying time in php [message #179376 is a reply to message #179375] |
Sun, 14 October 2012 10:25   |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sun, 14 Oct 2012 11:45:37 +0200, houghi wrote:
> I have a form that has the following entry fields:
> hours minutes seconds
>
> I need the time in seconds and that works:
> $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds'];
>
> However people can fill out nothing or they could enter characters
>
> What is the easiest way to verify if what the people filled out is
> correct? I have tried, but I get lost in the if statements.
Verify it is correct in what way?
If you want to record the time the data was entered, it would make more
sense to use the system time, and not collect the data in the form at all.
If you want the user to specify the time that an event happened in the
past, or is to happen in the future, give them a select list with options
of 1-31 for day, Jan-Dec for month, <whatever range suits> for year,
00-23 for hours, 00-59 for mins, 00-59 for seconds. Convert the form data
to an actual timestamp and do whatever validation and verification you
want.
Rgds
Denis McMahon
|
|
|
|
Re: Verifying time in php [message #179378 is a reply to message #179376] |
Sun, 14 October 2012 11:50   |
houghi
Messages: 45 Registered: September 2011
Karma: 0
|
Member |
|
|
Denis McMahon wrote:
>> What is the easiest way to verify if what the people filled out is
>> correct? I have tried, but I get lost in the if statements.
>
> Verify it is correct in what way?
>
> If you want to record the time the data was entered, it would make more
> sense to use the system time, and not collect the data in the form at all.
>
> If you want the user to specify the time that an event happened in the
> past, or is to happen in the future, give them a select list with options
> of 1-31 for day, Jan-Dec for month, <whatever range suits> for year,
> 00-23 for hours, 00-59 for mins, 00-59 for seconds. Convert the form data
> to an actual timestamp and do whatever validation and verification you
> want.
Sorry for the confiusion. I hope the link I gave clariefied things a
bit. http://houghi/gopro.php
You can enter the number of hours, minutes and seconds yourself.
So it is to verify if the human entry is correct.
This is used for calcualtions.
Say that somebody would like to do a timelaps over a period of 8 hours
with an interval of 1 image every second and a framrate of 30. This
would result in a movie that is 16 minutes long.
The time can be almost anything. You could have a project that takes 10
days (240 hours) and with a 60 second interval that would be an 8 minute
movie.
So one can fill out hours, minutes and seconds. However if nothing is
filled out, it is not correct. At least one must be filled out.
Also it must be numbers and not nececerily limited to 59 minutes. If you
have a detail of 117 minutes, you should be able to do that and not
first calculate back to 1 hour and 57 minutes.
So dropdown is not an option (And I dislike dropdown options for
minutes)
So it is not so much time as it is duration.
I am able to test each if there is a numeric field. I can't get the
combination of the fields working:
1) One of the time fields must be filled out.
2) None of the time fields must contain non-digits.
houghi
--
Dr. Walter Gibbs: Won't that be grand? Computers and the programs
will start thinking and the people will stop.
-- Tron (1982)
|
|
|
|
|
Re: Verifying time in php [message #179381 is a reply to message #179375] |
Sun, 14 October 2012 15:44   |
Richard Damon
Messages: 58 Registered: August 2011
Karma: 0
|
Member |
|
|
On 10/14/12 5:45 AM, houghi wrote:
> I have a form that has the following entry fields:
> hours
> minutes
> seconds
>
> I need the time in seconds and that works:
> $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds'];
>
> However people can fill out nothing or they could enter characters
>
> What is the easiest way to verify if what the people filled out is
> correct? I have tried, but I get lost in the if statements.
>
> Extra difficult is that they must not fill out all three. Only one is
> enough. e.g.
> 1 hour and no minutes or second gives 3600
> 1 hour, 1 minute and 1 second gives 3661
> 1 hour and 'X' should give an error.
> All empty should give an error
> empty hour 70 minutes and 75 seconds gives 4275.
> Hours of more then 24 will be possible as well.
> I might even add number of days.
>
> If the last one gives an error, but it makes the life much easier, I can
> live with that
>
> It is the fact that they do not need to fill out all three that I am
> stuck.
>
> What I have tried is first to see if all are empty
> Next try to see if each one is not a number
> Then I need to figure out if there is a combination of some of it.
>
> And that is when I get lost in the if statements and loops in loops.
>
> I am sure there is an easy way to tell if all is nice and works well,
> but how? Seaching google gave me transfering from seconds to H:i:s, but
> that is not what I want.
>
> houghi
>
First you need a definition of "Valid", it sounds like for your case,
the definition would be that each field contains only the characters 0-9
or is blank (which in some sense is a special case of only containing
the characters 0-9), with a second restriction of all can not be blank.
From your later discussion, it sounds like a result of all fields 0 or
blank is also invalid, for which the simplest test is probably do you
calculation and then see if the result is 0.
You might also want the fields to contain a possible single decimal
point so the person could enter something like 3.5 hours as a time. The
biggest question with this option is do you need to support
internationalization, where sometimes a decimal point is a '.' and
sometimes it is a ','.
The simplest structure for this sounds to be code that first checks each
field that is contains the proper characters, the compute the results
and see if the value is 0.
To fully test a field, if we are willing to ignore internationalization
(that is left as an exercise to the reader) is to match the fields to
the regular expression (warning, untested): ^\d*(\.\d*)?
You may also want to modify the test string to allow whitespace before
and after the string of digits.
|
|
|
|
|
|
|
|
|
Re: Verifying time in php [message #179388 is a reply to message #179386] |
Sun, 14 October 2012 17:41   |
houghi
Messages: 45 Registered: September 2011
Karma: 0
|
Member |
|
|
Martin Leese wrote:
>>
>> Challenging curcumstance: "A" "B" "C" cast to int becomes 0:0:0, which
>> is a valid time.
>
> But not a valid *duration* (which is what
> the OP actually wants).
Indeed and that is where I got stuck several times. So I started
thinking outside the box and for the test I do not care what the values
are, as long as they are values.
I then came up with if 1, 2 and 3 are ok, then 123 is OK as well.
If 4, Y and <empty> is not OK, then 4Y is not ok either.
As far as internationalisation goes, a dot will fork, a comma won't.
Even though I live in a country where should use a comma, I always se a
dot.
houghi
--
This is written under the inluence of the following:
> Artist : Santana
> Song : Europa (Earth's Heaven's Smile)
> Album : Moonflower
|
|
|
|
|
Re: Verifying time in php [message #179391 is a reply to message #179385] |
Sun, 14 October 2012 19:03   |
Michael Fesser
Messages: 215 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
.oO(Peter H. Coffin)
> On Sun, 14 Oct 2012 17:50:48 +0200, Michael Fesser wrote:
>> .oO(houghi)
>>
>>> I am aware of those. However I can not figure out how to do a check on
>>> all of them, without getting lost in if statements.
>>>
>>> See below where '-' means empty, 1 means any digit(s) and A means any no
>>> digit(s). I have not shown all options.
>>> [...]
>>
>> I wouldn't care too much about invalid inputs. Instead I would just take
>> what I get and see what I can do with it.
>>
>> So I would explicitly cast all values to int (invalid or empty values
>> will become 0), then just do a simple range check on all of the three
>> values and then do the math.
>
> Challenging curcumstance: "A" "B" "C" cast to int becomes 0:0:0, which
> is a valid time.
Yes, as expected and intended.
In my scripts I do a similar thing with date inputs. I don't like
separate fields for day, month and year. So I simply accept what I get
from a single field and try to parse it in several ways. If the result
is a valid date, I accept it. If not, it triggers an error message and
shows the form again.
And I would do the same here as described above. If all three values are
empty or invalid, the result will automatically be 0 and can easily be
rejected. And if values like 36h or 90m are accepted as well, even the
range check could be omitted: just cast to int, maybe abs(), and then
calculate the time in seconds, that's it. Why make things more difficult
than necessary?
Micha
--
http://mfesser.de/
Fotos | Blog | Flohmarkt
|
|
|
Re: Verifying time in php [message #179392 is a reply to message #179375] |
Sun, 14 October 2012 19:28   |
Luuk
Messages: 329 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 14-10-2012 11:45, houghi wrote:
> I have a form that has the following entry fields:
> hours
> minutes
> seconds
>
> I need the time in seconds and that works:
> $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds'];
>
> However people can fill out nothing or they could enter characters
>
> What is the easiest way to verify if what the people filled out is
> correct? I have tried, but I get lost in the if statements.
>
> Extra difficult is that they must not fill out all three. Only one is
> enough. e.g.
> 1 hour and no minutes or second gives 3600
> 1 hour, 1 minute and 1 second gives 3661
> 1 hour and 'X' should give an error.
> All empty should give an error
> empty hour 70 minutes and 75 seconds gives 4275.
> Hours of more then 24 will be possible as well.
> I might even add number of days.
>
> If the last one gives an error, but it makes the life much easier, I can
> live with that
>
> It is the fact that they do not need to fill out all three that I am
> stuck.
>
> What I have tried is first to see if all are empty
> Next try to see if each one is not a number
> Then I need to figure out if there is a combination of some of it.
>
> And that is when I get lost in the if statements and loops in loops.
>
> I am sure there is an easy way to tell if all is nice and works well,
> but how? Seaching google gave me transfering from seconds to H:i:s, but
> that is not what I want.
>
> houghi
>
whats wrong with this code:
<?php
$h=13;
$m=14;
$s=15;
$t=sprintf("%02d:%02d:%02d", $h, $m, $s);
$x=strtotime($t);
print $x."\n";
print date("l dS F Y H:i:s ",$x)."\n";
You only need some more validation on the 'strtotime' and 'date'
functions.....
|
|
|
|
|
Re: Verifying time in php [message #179395 is a reply to message #179378] |
Sun, 14 October 2012 21:12  |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Sun, 14 Oct 2012 13:50:51 +0200, houghi wrote:
> Denis McMahon wrote:
>>> What is the easiest way to verify if what the people filled out is
>>> correct? I have tried, but I get lost in the if statements.
>>
>> Verify it is correct in what way?
>>
>> If you want to record the time the data was entered, it would make more
>> sense to use the system time, and not collect the data in the form at
>> all.
>>
>> If you want the user to specify the time that an event happened in the
>> past, or is to happen in the future, give them a select list with
>> options of 1-31 for day, Jan-Dec for month, <whatever range suits> for
>> year, 00-23 for hours, 00-59 for mins, 00-59 for seconds. Convert the
>> form data to an actual timestamp and do whatever validation and
>> verification you want.
>
> Sorry for the confiusion. I hope the link I gave clariefied things a
> bit. http://houghi/gopro.php You can enter the number of hours, minutes
> and seconds yourself.
> So it is to verify if the human entry is correct.
> This is used for calcualtions.
>
> Say that somebody would like to do a timelaps over a period of 8 hours
> with an interval of 1 image every second and a framrate of 30. This
> would result in a movie that is 16 minutes long.
>
> The time can be almost anything. You could have a project that takes 10
> days (240 hours) and with a 60 second interval that would be an 8 minute
> movie.
>
> So one can fill out hours, minutes and seconds. However if nothing is
> filled out, it is not correct. At least one must be filled out.
> Also it must be numbers and not nececerily limited to 59 minutes. If you
> have a detail of 117 minutes, you should be able to do that and not
> first calculate back to 1 hour and 57 minutes.
> So dropdown is not an option (And I dislike dropdown options for
> minutes)
>
> So it is not so much time as it is duration.
>
> I am able to test each if there is a numeric field. I can't get the
> combination of the fields working:
> 1) One of the time fields must be filled out.
> 2) None of the time fields must contain non-digits.
OK, given three values in your form:
<p>Hours: <input type="text" size="4" name="hours"><br>
Minutes: <input type="text" size="4" name="mins"><br>
Seconds: <input type="text" size="4" name="secs"></p>
Then:
<?php
$secs = 0;
if ( isset( $_POST["hours"] ) )
$secs += 3600 * intval( $_POST["hours"] );
if ( isset( $_POST["mins"] ) )
$secs += 60 * intval( $_POST["mins"] );
if ( isset( $_POST["secs"] ) )
$secs += intval( $_POST["secs"] );
// $secs is now the combined value of
// whatever your hours, minutes and
// seconds inputs were, apply some limits
$secs = max( $secs, 1 );
$secs = min( $secs, 86400 );
// now if you want you can normalise $secs
// into hours : minutes : seconds
$hh = $secs / 3600;
$mm = ( $secs - $hh * 3600 ) / 60;
$ss = ( $secs - $hh * 3600 ) % 60;
$timestr = sprintf( "%02d:%02d:%02d", $hh, $mm, $ss );
?>
Unless I've totally misunderstood the problem, which is quite possible.
Rgds
Denis McMahon
|
|
|