Skip to content
Advertisement

PHP how to convert number (year) to text ex. 1996 = Ninteen Ninty six, and 2004 = Two thousand four

I have a certificate I want to convert year to text but in the given format

  convertYearToText(1994){
    return "Ninteen hundred ninty six";
    }
    convertYearToText(2004){
    return "two thousand four";
    }

I have a function but it gives me One Thousand Nine Hundred Ninety-Six

 numberTowords(1996)
    {

        $num = str_replace(array(',', ' '), '' , trim($num));
        if(! $num) {
            return false;
        }
        $num = (int) $num;
        $words = array();
        $list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
            'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
        );
        $list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
        $list3 = array('', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
            'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
            'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
        );
        $num_length = strlen($num);
        $levels = (int) (($num_length + 2) / 3);
        $max_length = $levels * 3;
        $num = substr('00' . $num, -$max_length);
        $num_levels = str_split($num, 3);
        for ($i = 0; $i < count($num_levels); $i++) {
            $levels--;
            $hundreds = (int) ($num_levels[$i] / 100);
            $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ' ' : '');
            $tens = (int) ($num_levels[$i] % 100);
            $singles = '';
            if ( $tens < 20 ) {
                $tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' );
            } else {
                $tens = (int)($tens / 10);
                $tens = ' ' . $list2[$tens] . ' ';
                $singles = (int) ($num_levels[$i] % 10);
                $singles = ' ' . $list1[$singles] . ' ';
            }
            $words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );
        } //end for loop
        $commas = count($words);
        if ($commas > 1) {
            $commas = $commas - 1;
        }
        return implode(' ', $words);
    }

I need the return result to be “nineteen hundred ninety-six” please help

Advertisement

Answer

I took the liberty of rewriting and cleaning up your function, adding one crucial part: the conversion from

(a) One Thousand Nine Hundred Ninety Six to (b) Nineteen Hundred Ninety Six.

The function now accepts a optional second argument $year. If set to true, it will return (b), otherwise (a).

numberTowords(1996) will give (a)

numberTowords(1996,true) will give (b)

The comments in de code show what I’ve changed

function numberTowords($num,$year=false){
    $num = str_replace(array(',', ' '), '' , trim($num));
    if($num==='')return false;
    
    $num = (int) $num;
    $words = [];
    $list=[
        1=>['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
        'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
        2=>['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred'],
        3=>['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion','octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion','quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion']
        ];
    
    //$levels = number of parts of 3 
    $levels = ceil(strlen($num)/3);
    if($levels===0)return false;

    //$parts = parts of 3, first one left-padded with zeros
    $parts = str_split(substr('00' . $num, -$levels*3), 3);
    $part_count=count($parts);

    //THE IMPORTANT YEAR BIT
    //only if year-flag = true
    //only if no more then 2 parts
    //only if year < 9999
    // EXAMPLE: 1986
    $change = false; //<< === added flag
    if($year===true && $part_count===2 && $num<9999){
        //get the first digit of last part (ex: 9)
        $x=substr($parts[$part_count-1],0,1);
        //if first digit = 0 : skip
        //else: remove from last part and add to part before
        // ex: 001 => 0019 and 986 => 86
        if($x!=='0'){
            $parts[$part_count-2]=$parts[$part_count-2].$x;
            $parts[$part_count-1]=substr($parts[$part_count-1],1);
            }
        }

    //LOOP THE PARTS
    for ($i=0; $i < $part_count; $i++) {
        $w=[];
        //get the (int) of part
        $part_num=(int)$parts[$i];
    
        //HUNDREDS
        if($part_num >= 100){
            $w[]=$list[1][(int)substr($parts[$i],0,1)];
            $w[]=$list[2][10];
            }
        //TENS and SINGLES
        $remainder=$part_num%100;
        if($remainder>19){
            $w[]=$list[2][floor($remainder/10)];
            $w[]=$list[1][$remainder%10];
            }
        else{
            $w[]=$list[1][$remainder];
            }
                // << TEST FOR FLAG
        if($change===true && $i===0)
            $w[]=$list[2][10];
        else 
            $w[]=$list[3][$part_count - $i -1];
        
        $words[]=implode(' ',$w);
    } //end for loop
    return implode(' ', $words);
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement