Sometimes, you want your dates to look like this: 3/3/2019. Other times, you want them to look like this: 03/03/2019.

There are plenty of wrong ways to do this. There are far fewer right ways to do it, and they mostly boil down to “use your language’s date library.”

And then there’s this, which “QuakePhil” found.

    function convertSingleDigitMonthOrDayToTwoDigitMonthOrDay($date) {
        $month = '';
        $day = '';
        $secondChar = substr($date, 1, 1);
        $thirdChar  = substr($date, 2, 1);
        $fourthChar = substr($date, 3, 1);
        $fifthChar  = substr($date, 4, 1);

        if ($secondChar === '/') {
            $month = '0' . substr($date, 0, 2);
        } else if ($secondChar !== '/') {
            $month = substr($date, 0, 3);
        }

        if ($thirdChar === '/' && $fifthChar === '/') {
            $day = '0' . substr($date, 1, 2);
        } else if ($secondChar === '/' && $fourthChar === '/') {
            $day = '0' . $thirdChar . '/';
        } else if ($secondChar === '/' && $fifthChar === '/') {
            $day = substr($date, 2, 3);
        } else if ($thirdChar === '/' && !in_array('/', array($secondChar, $fourthChar, $fifthChar))) {
            $day = substr($date, 3, 3);
        }

        return $month . $day;
    }

It’s worth noting that this project had just undergone a massive refactoring effort, and somehow this particular method snuck through.

It’s an… interesting approach to the logic. Instead of splitting the string on "/" (which would still be wrong, but at least make sense), we check which position the "/" falls. But we don’t do that by searching the string or indexing the string, but by doing a pile of substr calls and saving the result.

Phil suggested changing the code to something more reasonable, but since there had just been a massive refactoring project, nobody want to make any code changes which weren’t new features.