Ok, i’m a regex doofus. I only ever learn enough to get me by, but i would like to know how to do the below regex without using a php ternary operator in the middle of it. Also it may be ugly but it works…
'^". $date[0].(($date[0] == 0)?'?':'').$date[1]."[/.-]".$date[2].(($date[2] == 0)?'?':'').$date[3]."'";
uhm. as you can see im using a ternary operator to test if the date contains a 0. if it does.. then it can be ignored, if not then its a 1 or 2 and do not ignore. i’m sure there is a way to write this with just regex? If you know how then please tell me and i will be forever greatful. Also, just incase you wondering this is a regex in a mysql query.
Are you looking for this? match[1] is the month, match[2] is the day and match[3] is the year
You have a couple problems here.
First, never ever put a text into a database that you haven’t made sure is formatted correctly. The fact that the dates are in the database with and without the 0’s is a problem. Why aren’t the dates in datetime fields where the database would make sure they are formatted correctly?
Second, the regex above only works in the US where ‘mm/dd/yyyy’ dates are used. Support for other date formats is impossible if you are tied to that regex.
Third, why are you doing this in the database? Your example is WHERE birthday regex something…. That means you have this date before you are passing it to mySQL. So fix the date in PHP first: