CodeSOD: Never Refuse a Fifth

This post was originally published on this site

The Daily WTF

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

To read the full article click on the 'post' link at the top.