karel pitra wrote:
> 
> hi,
> 
> first - using php wouldn't it be much easier to use an integer to
> store datetime in the database? - i think it's faster and it allows you to
> manipulate the date much more easily.
> 
> you could just tokenize the date string (using space,'/' and '-' or any ohter
> characters as the separators) - you would get an array of numerical values
> where the first one would be years, second one moths an so on.
> 
> On Thu 16. August 2001 08:33, Van wrote:
> > Greetings:
> >
> > Building a web form.  There are 3 datetimes:  RequestDate, FulfillDate, and
> > CancelDate.
> >
> > New record populates a web form with the current date/time in yyyy-mm-dd
> > hh:mm:ss format.  RequestDate is current date, FullFillDate and CancelDate
> > are "0000-00-00 00:00:00".
> >
> > Updates present them with a record with the yyyy-mm-dd hh:mm:ss formatting
> > directly selected from the mysql_query.
> >
> > If the end-user doesn't manipulate the dates, all goes fine.  Just updates
> > the record with the current values in the web form.
> >
> > If they want to change one of these dates, they might be inclined to put
> > slashes in their dates rather than hyphens.  They also might want to just
> > put the day and the month (no time and no year).
> >
> > Form submission goes through and I need to put it into yyyy-mm-dd
> > (optionally) hh:mm:ss format to update the records.
> >
> > I'm sure this is a common problem, but I haven't seen a decent PHP
> > implementation of how to error check this and the UNIX_TIMESTAMP(date)
> > function doesn't like it when the year isn't entered first (for an American
> > implementation).  So, if the user types 8/15 and hits update, it goes to
> > 0000-00-00 00:00:00.
> >
> > Best I've come up with is to check for a length of 19 chars, and do some
> > processing in there.  Chances are there'll only be one slash so an ereg can
> > determine that and strip the first 2 argv's, pad the rest with zeroes.
> >
> > Anyone out their have a more complete and elegant way of doing this with a
> > good yyyy-mm-dd hh:mm:ss ereg check?
> >
> > Would love to do it cleanly.
> >
> > Thanks,
> > Van
> >
> > =========================================================================
> > Linux rocks!!!   http://www.dedserius.com
> > =========================================================================

Karel:

Thanks for the reply.  DateTime is plenty efficient for most purposes.  The
problem isn't storage and retrieval from the db, but what the end user does with
that value once it's generated into the form.  They could type anything.  

The topic addresses (or attains to address) the problem of giving them a date
format that will be acceptable for a mysql update.  But, people type crazy
stuff.  Consider "date/mo/dy hr:mn:sc" being typed in.  Stranger things have
happened.

The question I broached was:  "How do you responsibly deal with garbage?"  If
they type in the month/date, I can deal with that.  MySQL will insert anything
useful, if useful.  I'll end this discussion, here.

The problem was solved with the following PHP function:

Function DateChecker($DateString)
/*      2001-08-15 23:02:15:  Van
        Validate the DateString to be a valid date
        Ideally, it should be in yyyy-mm-dd hh:mm:ss format
*/
{
        $DateString = ereg_replace("/","-",$DateString);
        if ($DateString)
        /* If a value is passed for DateString */
        {
                if (strlen($DateString) == 19)
                /* If it's the correct length it might be good.
                */
                {
                        $aux = explode ("-", $DateString);
                        if (!checkdate($aux[1],$aux[2],$aux[0]))
                        /* Check to see if they switched from yyyy-mm-dd to 
                                mm-dd-yyyy format
                                Zero it out if so.
                        */
                        {
                                $DateString = "0000-00-00 00:00:00";
                        }
                } else {
                        /* Strange length.  Let's just grab the first 2 fields 
                           around "-" and hope it's month/day */
                        $aux = explode ("-", $DateString);
                        $DateString = date("Y")."-".$aux[0]."-".$aux[1]."
00:00:00";
                }
        } else {
        /* Otherwise zero it out */
                $DateString = "0000-00-00 00:00:00";
        }
        return($DateString);
}

It may be buggy, but I'd check it out before making that allegation.

I'd love to hear back if so.

I posted this to the MySQL AB list at [EMAIL PROTECTED], and offer it up to
the GPL license, if that's of value to anyone.  Use it, or abuse it at your
discretion.  Not warranted to be useful for any purpose, at all.  You can't sue
me if you use it and it breaks anything; my liability limited to 1 American
Penny if you own the trademark "Ded Serius Music", but you can use it freely,
subject to GPL-2, otherwise, which states that you still can't sue me if you try
it and don't like it.

Best Regards,
Van
-- 
=================================================================
Linux rocks!!!   http://www.dedserius.com/
=================================================================

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to