如何用ireport将日期转换为单词(字母)


How to turn day number to a word (letters) with ireport?

我目前正在iReport 5.50中创建一个正式文档。它的末尾必须有这样一行:"(…)本文件于2014年7月7日延期"。

"两个星期四和十四日的七月七日"是今天。我想知道是否有一种方法可以像文档需要的那样用文字显示当前日期。到目前为止,我只知道如何获得星期几的名称和月份的名称。。。

非常感谢您的帮助!

尽管您想要的内容与日期有关,但这是一个相当普遍的问题。

你想要的是将一个数字转换为它的序数等价物,这是一个经典的编程练习。

以下是一些PHP代码示例(我在原始代码中修复了一些问题):

function numToOrdinalWord($num)
{
    $first_word = array('eth', 'First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth',
         'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth', 'Thirteenth',
         'Fourteenth', 'Fifteenth', 'Sixteenth', 'Seventeenth', 'Eighteenth',
         'Nineteenth', 'Twentieth');
    $second_word = array('', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety');
    if ($num <= 20) return ($num == 0) ? 'Zeroth' : $first_word[$num];
    $first_num = substr($num, -1, 1);
    $second_num = substr($num, -2, 1);
    return $string = str_replace(
        'y-eth',
        'ieth',
        $second_word[$second_num] . '-' . $first_word[$first_num]
    );
}

实时演示|原始源