Thursday 6 September 2012

PHP Function To List Months Names and Years


/**
 * Return the List of all the 12 months name as per requested.
 * @param char $monthType The month format type you want month to be display.
 * @return array:string Return the list of months.
 */
function get_all_months($monthType = 'F')
{
 $months = array();
 
 for ( $m=1; $m<=12; $m++ )
 {
  $months[$m] = date($monthType, mktime(0,0,0,$m,1,2000));;
 }
 
 return $months;
}

/**
 * Return the List of all the year name as per requested.
 * @param char $yearType The Year format type you want year to be display.
 * @return array:string Return the list of month for upto 2020 years which you can change as per your needs.
 */
function get_all_years($yearType = 'Y')
{
 $months = array();
 $yearLength = 20; // This is the limit for year to be display for next 20 years.
 
 for ( $y=11; $y<=$yearLength; $y++ )
 {
  $months[$y] = date($yearType, mktime(0,0,0,1,1,$y));;
 }

 return $months;
}

No comments:

Post a Comment

Please post any queries and comments here.