Reputation: 8085
Working on a way to convert the number part of a UK licence plate to that of the reg year.
The aim is to take e.g 55 / 03 / 62
And output how they should be: 2005 / 2003 / 2012
My initial plan was this:
function convertPlateYearToYYYY($vrmYear) {
$firstDigit = substr($vrmYear, 0, 1);
$secondDigit = substr($vrmYear, 1, 1);
if ($firstDigit == 6) {
$partOne = '1';
} else {
$partOne = '0';
}
$date = "20" . $partOne . $secondDigit;
return $secondDigit;
}
$vrmYear = '05'
echo convertPlateYearToYYYY($vrmYear);
This works fine, but if submitting a value that starts with 0 (like in the example) the substr seems to fail and doesnt return anything. How do I overcome this?
Upvotes: 0
Views: 162
Reputation: 5114
First of all you're using return $secondDigit;
when I think you want to do return $date;
in your function.
Without going into further discussion about your question, here's a working solution accessing the first and second digits of your string as an array instead of using substr:
function convertPlateYearToYYYY($vrmYear) {
$firstDigit = $vrmYear[0];
$secondDigit = $vrmYear[1];
if (6 == $firstDigit) {
$partOne = '1';
} else {
$partOne = '0';
}
$date = "20{$partOne}{$secondDigit}";
return $date;
}
$vrmYear = '05';
echo convertPlateYearToYYYY($vrmYear);
Upvotes: 2
Reputation: 88657
Your current approach is wrong, as it treats the second 6 months of the plate year as an explicit special case. In fact one can use a simple bit of maths to calculate this across the entire range of valid plates in the current UK system, without any need to handle the leading digit specially.
function convert_uk_reg_year_to_yyyy($year)
{
// Normalise year to padded with 1 leading zero, if necessary. This ensures that
// if the input is int(9) instead of string(2) "09" then it will still work as
// expected.
$year = str_pad((int)$year, 2, 0, STR_PAD_LEFT);
// Make sure input is within valid range
if ($year < 0 || $year > 99) {
return false;
}
// Create result
return "20" . ($year[0] % 5) . $year[1];
}
This uses the modulus operator to figure out the "tens" component, and then simply uses the second digit of the input as the "units" component.
This will work for every plate year up to the end of the capacity of the current system (assuming we continue to use that system until 2049, when the plates run out).
PS. yes I did cheat by living in the UK and knowing how the number plate system works. It'll get more interesting when you start wanting to handle the old style leading-alpha plates, or the even older trailing-alpha plates, as these were much more disorderly in terms of the boundary between cycles.
Unfortunately this is still imperfect, as IIRC the new system actually came in to effect with the 50 plate and not the 00, but this could be special-cased. I'm unsure as to whether any personal plates that match the current system were issued before it came into effect, so matching the plate pattern is not really an effective solution, especially considering that plates may be transferred between vehicles. If you are building something that needs to accurately know the year of the car that the plate is currently attached to, then you need to use data from the DVLA (who do have APIs for this sort of thing).
Upvotes: 1
Reputation: 343
I fixed the missing semicolon and returned $date and it seems to work as intended
<?php
function convertPlateYearToYYYY($vrmYear) {
$firstDigit = substr($vrmYear, 0, 1);
$secondDigit = substr($vrmYear, 1, 1);
if ($firstDigit == 6) {
$partOne = '1';
} else {
$partOne = '0';
}
$date = "20" . $partOne . $secondDigit;
return $date;
}
$vrmYear = '05';
echo convertPlateYearToYYYY($vrmYear);
?>
Upvotes: 0