Tuesday, April 28, 2009

how to convert month from string to integer or opposite


smoetimes i need to print out the month in integer value like (1,2,3,4,5,6,7,8,9,10,11,12) or print out it in string value like April , March ,.... etc . i made class convert_month to convert any month in integer to string value or any month string value to integer see my example below :
make month_convert.php page and copy the code below inside it then save the page


class convert_month {

/*author engineer sherif sakr ,
//email :xxsherif82@yahoo.com


*/

//data members
private $msg ;
private $month;
private $type ;
private $montharray=array(1=>'January',2=>'February',3=>'March',
4=>'April',5=>'May',6=>'June',
7=>'July',8=>'August',9=>'September',
10=>'October',11=>'November',12=>'December');

//constructor function
public function __construct($month_from_user) {
//intiate month value
$this->month=$month_from_user;

// check month value type if integer or sting
$type= $this->get_variable_type() ;

// *******convert the value type to its oppsite ************//

if($type=="integer") $this->month=$this->convert_to_string() ;
else if($type=="string") $this->month=$this->convert_to_integer() ;

//if value type not supported get the error message
else return $this->msg ;
}

//********************* get method *************//
public function get_month() {
if($this->month==''){
return $this->error_msg();
}
else return $this->month ;
}

//************get variable type method ***********//
private function get_variable_type() {
$this->type=gettype($this->month) ;
return $this->type ;
}
//********convert variable type to integer method *********//

private function convert_to_integer() {
foreach ($this->montharray as $key=> $value ) {
if( $this->month==$value )
{
return $this->month=$key ;
break ;
}
}
}

//******convert variable type to string method **************//

private function convert_to_string() {
foreach ($this->montharray as $key=> $value ) {
if( $this->month==$key )
{
return $this->month=$value ;
break ;
}
}
}
//**************error method *****************//

public function error_msg() {

return $this->msg="sorry data type not supported !!! ";

}


}
?>

make test.php page and put the code below and save the page then run test.php

require "month_convert.php" ;

$month =4 ; // or put $month = 'april ' as string
$month= new convert_month($month);

$monthx=$month->get_month() ;

echo $monthx ; // will print April

?>

that was just example of using OOP but we can do the same using small function
put the code below in the page test2.php and run it

//get_month function
function get_month($month) {

//check case sensitive letter if type is string
if(gettype($month=='string')){
//convert to lower string
$month=strtolower($month);
//UPERCASE FIRST LETER ONLY
$month=ucfirst($month);
}


$montharray=array(1=>'January',2=>'February',3=>'March',
4=>'April',5=>'May',6=>'June',
7=>'July',8=>'August',9=>'September',
10=>'October',11=>'November',12=>'December');

/*check month
//in_arry($month,array) function searches an array for a specific value,
// This function returns TRUE if the value is found in the array, or FALSE otherwise.
//The array_key_exists($month,array) function checks an array for a specified key,
// and returns true if the key exists and false is the key does not exist. */

if(in_array($month,$montharray)==true || array_key_exists($month,$montharray)){
foreach ($montharray as $key=> $value ) {
if( $month==$value )
{
// return month back in integer value
return $month=$key ;
break ;

}

else if ( $month==$key )

{
// return month back in string value
return $month=$value ;
break ;

}
} // end forech

}
// return month back as messaage error with out
else return $month="not supported data" ;


} // end function

//test month
$month=3 ;

$month=get_month($month);

echo $month ; // print out March

?>

MS in Computer Science with paid training in USA company