Friday, August 12, 2011

[PHP] Number to alphabetical

This function is used to generate Excel report.

function toAlphaNumber( $num ) {
    $anum = '';
    while( $num >= 1 ) {
        $num = $num - 1;
        $anum = chr(($num % 26)+65).$anum;
        $num = $num / 26;
    }
    return $anum;
}

Usage:
echo toAlphaNumber(1) // output: A
echo toAlphaNumber(27) // output: AA
Related Posts Plugin for WordPress, Blogger...