Cristo Rutazihana Cristo Rutazihana - 4 months ago 10
PHP Question

First month number in a loop not being converted to a month name?

Good day, so I am developing a small school management system that requires me to store tuition fee installments into the database. Each installment has a due date and based on the due date I convert the month number into a month name. The code works perfectly fine however I've noticed that the first month number extracted from the first due date is not passing through my function that converts it to a month name. All the following month numbers have no issue, it is just the first one in the loop.

My function for adding installments is this one:

public function addInstallments($prepInt){

//extraction converted variables
extract($prepInt);

//divide total fee by the number of installments to get the value of the installments
$monthly_fee = round($fees/$number_of_installments,2);

/*
Retrieve course id of course
*/
$model = new CoursesModel();
$course_id = $model->returnCourseId($code);


/*
Save course start date in the due date array because it is going to be the base period for the due date calculations
*/
$due_date[0] = date('Y-m-d', strtotime("$start_date"));//base date for due date calculations


//save each installment into the database
for($i=1;$i<=$number_of_installments;$i++){


//add 30 days to course start date
$due_date[] = date('Y-m-d', strtotime("".$due_date[$i-1]." +30 days"));

//retrieve month name and year from due date
$month_int = date("m",strtotime($due_date[$i]));
$year = date("Y",strtotime($due_date[$i]));
$month_name = $this->returnNameofMonth($month_int);

$fieldset = "`course_id`='$course_id',`name`='Propina de $month_name $year',`amount_per_installment`='$monthly_fee',`due_date`='$due_date[$i]'";

$status = $model->CreateDb('installments',$fieldset);

}

return $status;


And this is my function for converting the month numbers into month names

/*
Returns name of the months
*/

public function returnNameofMonth($month_int){

switch($month_int){
case 01:
return 'Janeiro';
break;
case 02:
return 'Fevereiro';
break;
case 03:
return 'Março';
break;
case 04:
return 'Abril';
break;
case 05:
return 'Maio';
break;
case 06:
return 'Junho';
break;
case 07:
return 'Julho';
break;
case 08:
return 'Agosto';
break;
case 09:
return 'Setembro';
break;
case 10:
return 'Outobro';
break;
case 11:
return 'Novembro';
break;
case 12:
return 'Dezembro';
break;
}
}


N.B. Am using OOP

When I did some debugging on my own I discovered two things:
1. When I don't convert the month numbers into month names the month number of the first element of my loop is stored in my database;
2.The first element of the loop is unable to pass through my
returnNameOfMonth()
.

I did a second test of my code with some slight change in my
addInstallments()
function. With the new change I discovered that the function saved 'teste' as a month name in the database without any issues.

/*
Add installments
*/
public function addInstallments($prepInt){

//extraction converted variables
extract($prepInt);

//divide total fee by the number of installments to get the value of the installments
$monthly_fee = round($fees/$number_of_installments,2);

/*
Retrieve course id of course
*/
$model = new CoursesModel();
$course_id = $model->returnCourseId($code);


/*
Save course start date in the due date array because it is going to be the base period for the due date calculations
*/
$due_date[0] = date('Y-m-d', strtotime("$start_date"));//base date for due date calculations


//save each installment into the database
for($i=1;$i<=$number_of_installments;$i++){


//add 30 days to course start date
$due_date[] = date('Y-m-d', strtotime("".$due_date[$i-1]." +30 days"));

//retrieve month name and year from due date
$month_int = date("m",strtotime($due_date[$i]));
$year = date("Y",strtotime($due_date[$i]));
//this is the slight change I added
if($i==1){
$month_name = 'teste';
}
else{
$month_name = $this->returnNameofMonth($month_int);
}

$fieldset = "`course_id`='$course_id',`name`='Propina de $month_name $year',`amount_per_installment`='$monthly_fee',`due_date`='$due_date[$i]'";

$status = $model->CreateDb('installments',$fieldset);

}

return $status;
}


Conclusion. The problem seems to lie on my
returnNameOfMonth()
function but up to now I can't seem to figure out what is the issue. Please help me out.

Answer

If you need mount in m format ('01' .. '02') you shold use string for avoid implict conversion .. then try

     switch($month_int){
         case '01':
          return 'Janeiro';
          break;
         case '02':
          return 'Fevereiro';
         .... 

otherwise th could be implicitally converted in

switch($month_int){
         case 1:
          return 'Janeiro';
          break;
         case 2:
          return 'Fevereiro';
          ...