PHP Convert Decimal UK Currency To Pounds Shillings Pence
I came across an unusual problem the other day. Whilst developing a PHP book / magazine cataloguing system I wanted to output the cover prices of the items that were pre-decimalisation (1971) in the old UK pounds / shillings / pence format.
For a reminder, particularly for those born later than the mid sixties who will have no recollection of the olden days, the British currency had 12 pennies (d) to the shilling [s] and there were 20 shillings to the pound (£). That makes a total of 240 pennies in a pound.
My database was storing the cover prices in decimal double format, so a product costing £2.25 today would be stored as 2.25, whilst a product from the sixties with a cover price of 10/- (£0 10s 0d) is the same as 50p today, is stored as 0.50.
A quick routine in PHP to convert that to pounds / shillings / pence is reproduced below. Hope it's useful for anyone else out there dealing with old products!
PHP Decimal to Pounds Shillings Pence Converter
<?php
function convertlsd($amount) {
$pounds = floor($amount);
$total_pence = ($amount - $pounds) * 240;
$shillings = floor($total_pence / 12);
$pence = $total_pence % 12;
$output = '£' . $pounds . ', ' . $shillings . 's ' . $pence . 'd';
return $output;
}
?>