Strange Date Corruptions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Strange Date Corruptions

Post 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;
  }
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

remove the #s to enable the debug output again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Can you clarify these "strange things"?
Post Reply