<?php
/*
Plugin Name: Year2Roman
Plugin URI: http://wordpress.org/support/topic/37817
Description: Converts post date year to roman numeral format (or any number between 1 and 3999).
Author: Kaf Oseo
Version: 0.1
Author URI: http://szub.net/
Usage:
In The Loop:
<?php year2roman(); ?>
Pass your own number to convert:
<?php year2roman(2001); ?>
Assign to PHP variable:
<?php $romana = get_year2roman(); ?>
Combined with the_date():
<?php the_date('F j, ', '<h2>', get_year2roman() . '</h2>'); ?>
(Note: format string of 'F j, ' generates "June 29, ")
Based on code found at:
http://www.phpbuilder.com/lists/php-db/2003061/0349.php
Thanks to Hugh Bothwell, and the PHP community at large!
*/
function year2roman($num='') {
echo get_year_to_roman($num);
}
function get_year2roman($num='') {
if(!$num) {
global $post;
$num = substr($post->post_date,0,4);
}
$num = (int) $num;
if (($num < 1) || ($num > 3999))
return('No corresponding Roman number!');
$m = (int) ($num * 0.001); $num -= $m*1000;
$c = (int) ($num * 0.01); $num -= $c*100;
$x = (int) ($num * 0.1); $num -= $x*10;
$i = (int) ($num);
return (
RomanDigit($m, 'M', '', '')
.RomanDigit($c, 'C', 'D', 'M')
.RomanDigit($x, 'X', 'L', 'C')
.RomanDigit($i, 'I', 'V', 'X')
);
}
function RomanDigit($dig, $one, $five, $ten) {
switch($dig) {
case 0: return "";
case 1: return "$one";
case 2: return "$one$one";
case 3: return "$one$one$one";
case 4: return "$one$five";
case 5: return "$five";
case 6: return "$five$one";
case 7: return "$five$one$one";
case 8: return "$five$one$one$one";
case 9: return "$one$ten";
}
}
?>