It just amazed me to know that many PHP programmers are still using the old conventional tedious way of getting date and time. It reminds me of the Basic days.

Take a look at the codes below.

function shortMonthName($monthNum) {

if($monthNum == “01”)
$monthName = “Jan”;
elseif($monthNum == “02”)
$monthName = “Feb”;
elseif($monthNum == “03”)
$monthName = “Mar”;
elseif($monthNum == “04”)
$monthName = “Apr”;
elseif($monthNum == “05”)
$monthName = “May”;
elseif($monthNum == “06”)
$monthName = “Jun”;
elseif($monthNum == “07”)
$monthName = “Jul”;
elseif($monthNum == “08”)
$monthName = “Aug”;
elseif($monthNum == “09”)
$monthName = “Sep”;
elseif($monthNum == “10”)
$monthName = “Oct”;
elseif($monthNum == “11”)
$monthName = “Nov”;
else
$monthName = “Dec”;

return $monthName;
}

That’s a lot of lines there, in short, efficiency wise, the code is far from that. Below is a full rewrite of the code with the same output and fewer lines.

function sMonName($mDate){
return date(‘M’,strtotime(“$mDate”));
}

Given that the function expects the month number only then you can rewrite the code to something like the one below.


function sMonName($mNum){
return date(‘F’,strtotime(“1979-$mNum-1”));
}

Both returns the same output, you can play with the date format to achieve the desired format to return.

Have fun.

Leave a Reply

Your email address will not be published. Required fields are marked *