Code Comments
Programming Forum and web based access to our favorite programming groups.I'm working on a php front end to a mysql scheduling database. The
database is used to generate a calendar of work shifts. My code is
meant to allow staff members to enter their work shifts. The resulting
calendar already exists and works.
Part of the web form requires that staff members enter the start and
end times of their work shifts (these vary). They are stored in the
database in 24-hour time with a colon between the hours and minutes.
We use '24:00' and '00:00' to differentiate between different days in
the calendar. The times are stored in '$startt' and '$endt'.
So, a staff member might enter a $startt of 16:00 and an $endt of
24:00. Since it's my job to make sure everything is validated, I have
to check if the values are correct times (and not names or prices or
other errors).
I can't find any useful php functions that deal with clock time
without the corresponding 'date' information. So, my first question is
"Did I miss a function that would do this?"
Assuming I didn't miss a useful existing function, I then would like
some help with constructing my own.
The most minimal check seems to be something that would determine if
there's two numbers (between 00 and 24) to the left of a colon, and
two other numbers (between 00 and 59) to the right of a colon.
So, in pseudo-code, something like this:
list($hrs, $mins) = explode(":", $startt);
echo $hrs;
echo $mins;
and(gte(00),lte(24)),$hrs),and(gte(00),l
te(59),$mins))
Here is where I start to get really stuck. I'm used to list
manipulation using a language similar to LISP (although it's not
lisp), and php has been giving me quite a bit of mental trouble.
Gwen
Post Follow-up to this message
On 12 Nov 2004 07:29:50 -0800, gwen.morse@gmail.com (Gwen Morse)
wrote:
>Part of the web form requires that staff members enter the start and
>end times of their work shifts (these vary). They are stored in the
>database in 24-hour time with a colon between the hours and minutes.
>We use '24:00' and '00:00' to differentiate between different days in
>the calendar. The times are stored in '$startt' and '$endt'.
>
>So, a staff member might enter a $startt of 16:00 and an $endt of
>24:00. Since it's my job to make sure everything is validated, I have
>to check if the values are correct times (and not names or prices or
>other errors).
I got this working once I figured out that regexp matching was
functional within php.
//Check if the startt and endt are valid times:
function check_time($str)
{
//returns 1 if valid time, 0 if not
if (ereg("([0-1][0-9]|[2][0-4])(:[0-5][0-9])", $str))
return 1;
else
return 0;
}
if (check_time($HTTP_POST_VARS[startt]) == 0) {
echo "<br>The start time you entered is not valid. The
format is HH:MM";
}
elseif (check_time($HTTP_POST_VARS[endt]) == 0) {
echo "<br>The end time you entered is not valid. The
format is HH:MM";
}
else {
// if "everything" is correct, here is the submit sequence.
Gwen
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.