I’m looking to slightly modify the PHP date() function so that it recognises the UK 6 digit format of dd/mm/yy or dd-mm-yy. Understandably, it will think that a date which is in this form is instead in international (yy-mm-dd) format.
I still wish to pass it into the date function as this will allow it to recognise other formats, but as I’m based in the UK people are most likely going to put it in the UK format.
Any ideas? I suspect Regex is involved in the solution to this problem…
Update – my solution
Using parts from the answer below, this was how I was able to change it just in the case of dd/mm/yy:
$s_date = str_replace('/', '-', $s_date);
preg_match('^(.*)[-](.*)[-](.*)$^', $s_date, $matches);
if ($matches) {
if (strlen($matches[3]) == 2) {
$matches[3] = '20'.$matches[3];
}
$s_date = $matches[1].'-'.$matches[2].'-'.$matches[3];
}
This function will allow you to check for dd-mm-yy and dd/mm/yy.
By using a str replace – you force the date to always be in the ‘dd-mm-yy’ format. strtotime interprets dates with a “-” as the format you want.
Quote from strtotime: