我正在开发一个小型学校管理系统,该系统要求我将分期学费存储到数据库中。每一期都有一个到期日,根据到期日,我将月份编号转换为月份名称。
代码运行得很好,但我注意到,从第一个到期日提取的第一个月编号没有通过我的函数,该函数将其转换为月份名称。接下来的所有月份数字都没有问题,这只是循环中的第一个。
我添加分期付款的功能是:
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;
这是我将月份编号转换为月份名称的函数
/*
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.我使用OOP
当我自己调试时,我发现了两件事:
- 当我不将月份编号转换为月份名称时,循环的第一个元素的月份编号存储在我的数据库中
- 循环的第一个元素无法通过我的
returnNameOfMonth()
我对代码进行了第二次测试,对addInstallments()
函数进行了一些细微的更改。通过新的更改,我发现该函数将"teste"作为月份名称保存在数据库中,没有任何问题。
/*
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;
}
结论。问题似乎出在我的returnNameOfMonth()
函数上,但到目前为止,我似乎还不知道问题出在哪里。
简化returnNameofMonth函数,甚至完全删除它,只需使用date来派生月份的全名。如果你想保留该功能,请尝试:
/*
Returns name of the months
*/
public function returnNameofMonth($month_int){
return date('F',strtotime($month_int . '/01' . date('Y') . '/'));
您也可以直接在addInstallments函数中使用它:
$month_name = date('F',strtotime($month_int . '/01' . date('Y') . '/'));
如果您需要以m格式('01'..'02'(装载,您可以使用字符串来避免隐含的转换。。然后尝试
switch($month_int){
case '01':
return 'Janeiro';
break;
case '02':
return 'Fevereiro';
....
否则th可以在中进行隐含转换
switch($month_int){
case 1:
return 'Janeiro';
break;
case 2:
return 'Fevereiro';
...