|
|
|
| |
| <?php
/**
* This function will convert an input month to various formats (eg January, Jan, Ja, J, 1).
* It accepts a string or an array as input and returns a string or an array. Be careful
* when using an array with non-unique elements.
*
* @param mixed $month --> The month to be converted; a string or and array.
* @param int $format --> An integer specifying the format of the result (0->12, 1->D, 2->De, 3->Dec, 4->December).
* @return mixed --> string or array
*/
function convertMonth($month, $format)
{
# Check for valid parameters #
if ($format !== 0 && $format !== 1 && $format !== 2 && $format !== 3 && $format !== 4)
{
printf('Wrong parameter for $format. This must be 0, 1, 2, 3 or 4.');
exit();
}
$january = array(1, 'j', 'ja', 'jan', 'january');
$february = array(2, 'f', 'fe', 'feb', 'february');
$march = array(3, 'm', 'ma', 'mar', 'march');
$april = array(4, 'a', 'ap', 'apr', 'april');
$may = array(5, 'm', 'ma', 'may', 'may');
$june = array(6, 'j', 'ju', 'jun', 'june');
$july = array(7, 'j', 'ju', 'jul', 'july');
$august = array(8, 'a', 'ag', 'aug', 'august');
$september = array(9, 's', 'se', 'sep', 'september');
$october = array(10, 'o', 'oc', 'oct', 'october');
$november = array(11, 'n', 'no', 'nov', 'november');
$december = array(12, 'd', 'de', 'dec', 'december');
$monthsOfTheYear = array($january, $february, $march, $april, $may, $june, $july,
$august, $september, $october, $november, $december);
if (!is_array($month))
{
$month = array(strtolower($month));
}
$inputSize = count($month);
for ($i = 0; $i < 12; $i++)
{
$match = array_intersect($month, $monthsOfTheYear[$i]);
if (!empty($match))
{
if ($inputSize > 1)
{
$result[] = ucfirst($monthsOfTheYear[$i][$format]);
}
else
{
$result = ucfirst($monthsOfTheYear[$i][$format]);
return $result;
}
}
else
{
if ($i === 11)
{
printf('Wrong parameter for $month.');
exit();
}
}
}
return $result;
}
?> | | |