I cant quite get my head around what the ^ is doing in my preg_match.
if (preg_match('~^(\d\d\d\d)(-(\d{1,2})(-(\d{1,2}))?)?$~', trim($date), $dateParts)) {
echo the $dateparts and do some magic with them
} else {
tell me the date is formatted wrong
}
As I see it this is looking to see if the $date matches the format which I read as 4 decimals - 1 or 2 decimals - 1 or 2 decimals
if it does match then the IF statement displays the date, if it doesn't then it gives an error of incorrect date formatting.
However just passing it the year $date = '1977' with nothing else (no day or month) it still goes through as true and displays the dateparts, I would thought it would throw an error?
Can someone point out what I'm missing in the regular expression? I'm guessing it's the ^ or possibly the ?$ at the end may mean only match part of it?