Convert Month Name To Number Using Simple php

Last updated on May 23rd, 2016 at 04:48 pm

Convert month name to month number using simple php. Here i am using the simplest trick of IF conditon in php which doesn’t need any complex logic. Just converting JAN,FEB,MAR,APR,MAY…DEC accordingly. You can modify the script and change the month name according to your convenience. You can also use switch condition which will be much faster in processing compared to if.

function check($month)
{
if($month == 'Jan')
{
$val = 1;
}else
if($month == 'Feb')
{
$val = 2;

}else
if($month == 'Mar')
{
$val = 3;

}else
if($month == 'Apr')
{
$val = 4;

}else
if($month == 'May')
{
$val = 5;

}else
if($month == 'Jun')
{
$val = 6;
}else
if($month == 'Jul')
{
$val = 7;
}else
if($month == 'Aug')
{
$val = 8;
}else
if($month == 'Sep')
{
$val = 9;
}else
if($month == 'Oct')
{
$val = 10;
}else
if($month == 'Nov')
{
$val = 11;
}else
if($month == 'Dec')
{
$val = 12;
}
return $val;
}
echo "The example date is Jan 1, 2012 so the converted value for JAN will be ";
echo check(Jan);

DEMO

Click to rate this tutorial!
[Total: 4 Average: 3]

Leave a Reply

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