Page 1 of 1

Strange Date Corruptions

Posted: Mon Nov 06, 2006 2:40 pm
by facets
Hello All,
I'm maintaining someone else's code and and have found the following date conversion syntax.
Although it's not erroring i'm getting strange results. Anyone care to scrutinise?

Any date can be inputted, even an incorrect one, no error is returned and the defaults (0000-00-00) are entered into MySQL, which is obviously detecting the error.
Strange things are happening on months with only 28 or 30 days. On all other months the codes performs seamlessly.

Any pointers?

Also, I know I should probably use timestamps for dates but as I said, i didn't write this and would like to patch it up until time permits for a re-write.

ta, Will/

Code: Select all

function date_oz2us($date)
  {
    if ( ereg ( "(....)[-,/,_](..)[-,_,/](..)", $date, $regs ) )
    {
         #print "US date found<p>";
    }
    else if ( ereg ( "(.*)[-,/,_](.*)[-,/,_](.*)", $date, $regs ) )
    {
         #print "Warning: Ozzie date found<br>";
         #print "Reformatting to US date for data storage<p>";

         if ( strlen($regs[2]) == 1 ) { $regs[2] = "0" . $regs[2];}
         if ( strlen($regs[1]) == 1 ) { $regs[1] = "0" . $regs[1];}

         $date = $regs[3]. "-". $regs[2]. "-". $regs[1];
    }
    else
    {
         #print "Error: Date incorrectly formatted<p>";
    }

    return $date;
  }

Posted: Mon Nov 06, 2006 2:51 pm
by volka
remove the #s to enable the debug output again.

Posted: Mon Nov 06, 2006 2:54 pm
by feyd
Can you clarify these "strange things"?