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

Home » Imported messages » comp.lang.php » Verifying time in php
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Verifying time in php [message #179375] Sun, 14 October 2012 09:45 Go to next message
houghi is currently offline  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 Go to previous messageGo to next message
Denis McMahon is currently offline  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 #179377 is a reply to message #179375] Sun, 14 October 2012 10:30 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
houghi wrote:
> I have a form that has the following entry fields:
> hours
> minutes
> seconds

The page can be found here: http://houghi.org/gopro.php
The code can be seen here : http://houghi.org/gopro.php?code

What I want to know if the $realtime is something I could get an error
message form.

Please do not comment on the rest of the code inbthis tread. I know it
is ugly and wrong i other places.

I just want to figure out how I can check if Hours, Minutes and Seconds
input is valid.

houghi
--
This is written under the inluence of the following:
> Artist : James Brown
> Song : Get Up Offa That Thing (Release The Pressure)
> Album : Star Time
Re: Verifying time in php [message #179378 is a reply to message #179376] Sun, 14 October 2012 11:50 Go to previous messageGo to next message
houghi is currently offline  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 #179379 is a reply to message #179377] Sun, 14 October 2012 13:03 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 14/10/12 12:30, houghi wrote:
> houghi wrote:
>> I have a form that has the following entry fields:
>> hours
>> minutes
>> seconds
>
> The code can be seen here : http://houghi.org/gopro.php?code
>
> $realtime=$_POST['hours']*3600+$_POST['minutes']*60+$_POST['seconds'];
>
> What I want to know if the $realtime is something I could get an error
> message form.

You would in most cases have a value of some sort, but you can't always
relay on that it will for ever be like that (we know PHP makes some big
changes from time to time) and what happens further down if you have
negative values or huge values.

Validate what you get and make a sanity check on what you got before you
calculate the realtime value.


> I just want to figure out how I can check if Hours, Minutes and Seconds
> input is valid.

There are quite many is_* functions, among those you have
is_numeric()[1] and another one is isset()[2] which gives you true or false.


[1]: http://www.php.net/manual/en/function.is-numeric.php
[2]: http://www.php.net/manual/en/function.isset.php

--

//Aho
Re: Verifying time in php [message #179380 is a reply to message #179379] Sun, 14 October 2012 15:16 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
J.O. Aho wrote:
> Validate what you get and make a sanity check on what you got before you
> calculate the realtime value.
>
>
>> I just want to figure out how I can check if Hours, Minutes and Seconds
>> input is valid.
>
> There are quite many is_* functions, among those you have
> is_numeric()[1] and another one is isset()[2] which gives you true or false.
>
>
> [1]: http://www.php.net/manual/en/function.is-numeric.php
> [2]: http://www.php.net/manual/en/function.isset.php

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.

Valid? $hours $min $sec
No - - -
Yes 1 - -
Yes 1 - 1
Yes - 1 -
Yes 1 1 1
No A - -
No 1 A -
No 1A - -

So I have to figure out if some are filled out and if they are, if they
are digits. Looking around for 2 days now to make it intro something
can read next week as well.

I keep getting back to where I need to get through all options.

So is_numeric is obvious as well as is_set, but is_set is not needed if
others are set.

houghi
--
This is written under the inluence of the following:
> Artist : Apocalyptica
> Song : Hope (Vol.2) (Radio Edit)
> Album : Hope Vol.2
Re: Verifying time in php [message #179381 is a reply to message #179375] Sun, 14 October 2012 15:44 Go to previous messageGo to next message
Richard Damon is currently offline  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 #179382 is a reply to message #179380] Sun, 14 October 2012 15:50 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.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.

Micha

--
http://mfesser.de/
Fotos | Blog | Flohmarkt
Re: Verifying time in php [message #179383 is a reply to message #179380] Sun, 14 October 2012 15:52 Go to previous messageGo to next message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma: 0
Member
houghi wrote:
> However I can not figure out how to do a check on all of them,
> without getting lost in if statements.

How about:

$ok = 0;
if (is_valid($hours)) {$ok++;}
if (is_valid($min)) {$ok++;}
if (is_valid($sec)) {$ok++;}

if ($ok) {
// fine :-)
} else {
// report error
}

--
Christoph M. Becker
Re: Verifying time in php [message #179384 is a reply to message #179382] Sun, 14 October 2012 16:22 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 14.10.2012 17:50, schrieb Michael Fesser:
> .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.

+1

/Str.
Re: Verifying time in php [message #179385 is a reply to message #179382] Sun, 14 October 2012 16:41 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
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.

Use the values submitted to make a valid format to throw at strptime()
perhaps... If it returns FALSE, it's not a valid time. Anything else is.

--
It is impossible to sharpen a pencil with a blunt axe. It is equally
vain to try to do it with ten blunt axes instead
--E.W Dijkstra, 1930-2002
Re: Verifying time in php [message #179386 is a reply to message #179385] Sun, 14 October 2012 17:06 Go to previous messageGo to next message
Martin Leese is currently offline  Martin Leese
Messages: 23
Registered: June 2012
Karma: 0
Junior Member
Peter H. Coffin wrote:

> On Sun, 14 Oct 2012 17:50:48 +0200, Michael Fesser wrote:
>> 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.

But not a valid *duration* (which is what
the OP actually wants).

--
Regards,
Martin Leese
E-mail: please(at)see(dot)Web(dot)for(dot)e-mail(dot)INVALID
Web: http://members.tripod.com/martin_leese/
Re: Verifying time in php [message #179387 is a reply to message #179380] Sun, 14 October 2012 17:20 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
houghi wrote:
> Valid?$hours $min $sec
> No - - -
> Yes 1 - -
> Yes 1 - 1
> Yes - 1 -
> Yes 1 1 1
> No A - -
> No 1 A -
> No 1A - -
>
> So I have to figure out if some are filled out and if they are, if they
> are digits. Looking around for 2 days now to make it intro something
> can read next week as well.
>
> I keep getting back to where I need to get through all options.
>
> So is_numeric is obvious as well as is_set, but is_set is not needed if
> others are set.

Found finaly the solution. Instead of looking at the seperate values and
then trying to combine them, it is better to combine them and then
evaluate them with
$testtime=$_POST['hours'].$_POST['minutes'].$_POST['seconds'];

$hours $min $sec $testtime Valid?
- - - - No
1 - - 1 Yes
1 - 1 11 Yes
- 1 - 1 Yes
1 1 1 111 Yes
A - - A No
1 A - 1A No
1A - - 1A No

That way I can just do a 'is_numeric($testtime)' and no need to use
is_set or anything else.

houghi
--
This is written under the inluence of the following:
> Artist : Nightwish
> Song : Reach (Amaranth Demo Version)
> Album : Amaranth
Re: Verifying time in php [message #179388 is a reply to message #179386] Sun, 14 October 2012 17:41 Go to previous messageGo to next message
houghi is currently offline  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 #179389 is a reply to message #179387] Sun, 14 October 2012 18:41 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 14/10/12 19:20, houghi wrote:

> Found finaly the solution. Instead of looking at the seperate values and
> then trying to combine them, it is better to combine them and then
> evaluate them with
> $testtime=$_POST['hours'].$_POST['minutes'].$_POST['seconds'];
>
> $hours $min $sec $testtime Valid?
> - - - - No
> 1 - - 1 Yes
> 1 - 1 11 Yes
> - 1 - 1 Yes
> 1 1 1 111 Yes
> A - - A No
> 1 A - 1A No
> 1A - - 1A No
>
> That way I can just do a 'is_numeric($testtime)' and no need to use
> is_set or anything else.

an alternative is:

if you have <input name="variable[xxx]" ...> in your form

then you can in your script
$ok = true;
foreach($_POST['variable'] as $value) {
if(!is_numeric($value))
$ok = false;
}

even if your method works now, I wouldn't bet on it in the future.

--

//Aho
Re: Verifying time in php [message #179390 is a reply to message #179387] Sun, 14 October 2012 18:49 Go to previous messageGo to next message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma: 0
Member
houghi wrote:
> Instead of looking at the seperate values and
> then trying to combine them, it is better to combine them and then
> evaluate them with
> $testtime=$_POST['hours'].$_POST['minutes'].$_POST['seconds'];

Did you consider the following input values?

$hours $min $sec $testtime Valid?
0 x 12 0x12 Yes
-1 10 20 -11020 Yes
1 . 1 1.1 Yes
1 2e 34 12e34 Yes

--
Christoph M. Becker
Re: Verifying time in php [message #179391 is a reply to message #179385] Sun, 14 October 2012 19:03 Go to previous messageGo to next message
Michael Fesser is currently offline  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 Go to previous messageGo to next message
Luuk is currently offline  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 #179393 is a reply to message #179390] Sun, 14 October 2012 20:32 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
Christoph Becker wrote:
> Did you consider the following input values?
>
> $hours $min $sec $testtime Valid?
> 0 x 12 0x12 Yes
> -1 10 20 -11020 Yes
> 1 . 1 1.1 Yes
> 1 2e 34 12e34 Yes

No. Thanks for the pointer.

houghi
--
You are about to enter another dimension, a dimension not only of
sight and sound but of mind. A journey into a wondrous land of
imagination. Next stop, Usenet!
Re: Verifying time in php [message #179394 is a reply to message #179391] Sun, 14 October 2012 20:39 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
Michael Fesser wrote:
> 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?

I make things more difficult, because some people will always count in
minutes when making movies and others will not.
So 80 will be hours for one and minutes for another.

And then there will yet be others wo do 16:34. So will that be 16 hours
and 34 minutes or 16 minutes and 34 seconds?

houghi
--
You are about to enter another dimension, a dimension not only of
sight and sound but of mind. A journey into a wondrous land of
imagination. Next stop, Usenet!
Re: Verifying time in php [message #179395 is a reply to message #179378] Sun, 14 October 2012 21:12 Go to previous message
Denis McMahon is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Curl chmod file transfer problem...
Next Topic: test
Goto Forum:
  

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

Current Time: Sat Nov 23 11:41:47 GMT 2024

Total time taken to generate the page: 0.02832 seconds