Skip to content

Instantly share code, notes, and snippets.

@Digi92
Last active May 13, 2021 07:51
Show Gist options
  • Save Digi92/23432a987fba0422578cbcdcaad97b73 to your computer and use it in GitHub Desktop.
Save Digi92/23432a987fba0422578cbcdcaad97b73 to your computer and use it in GitHub Desktop.
This function will convert a number to an excel-like column name. For example: 1 => A 2 => B 27 => AA 28 => AB 14558 => UMX URL: https://stackoverflow.com/questions/3302857/algorithm-to-get-the-excel-like-column-name-of-a-number#answer-3302991
<?php
function getNameFromNumber($num) {
$numeric = $num % 26;
$letter = chr(65 + $numeric);
$num2 = intval($num / 26);
if ($num2 > 0) {
return $this->getNameFromNumber($num2 - 1) . $letter;
} else {
return $letter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment