Sunday, January 2, 2022

[FIXED] How to get value of form field and past it to another form field

Issue

I am developing HR system, I want to calculate how many days off the employee can take. In the HR table I have three attributes.

1- start_date(when the employee starting to work in the company).

2- leavestaken(how many days off the employee took before).

3- leaves(the number of days available for the employee to take off) which equal to=(months between current date and start_date) * 2.5(beacuse each month give 2.5 days off) - leavestaken

third attribute is automated and not from the user, how I can get the first and second attributive values from field forms, use them in the calculation and save the result in the leaves attribute?

I'm using sql and this only for server side


Solution

My solution was

if ($model->load(Yii::$app->request->post())  ) {

       $date = date('m/d/Y');

     $datetime1 = date_create($date); 
     $datetime2 = date_create($model->start_date); 

     // calculates the difference between DateTime objects 
     $interval = $datetime2->diff($datetime1);
     $n_month= $interval->m + 12*$interval->y;
     $model->leaves = ceil($n_month * 2.5) - $model->leavestaken;


        $model->save();


Answered By - Jassim Kadhem

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.