0

I'm trying to do a schedule like a loan.

So I have done the following code:

function periode($var, $i2)
    {
        if($var=='52')
        {
            echo '"+ '.$i2.' week"';
        }
        if($var=='26')
        {
            echo '"+ ('.$i2.' +1) week"';
        }
        if($var=='12')
        {
            echo '"+ '.$i2.' month"';
        }
        if($var=='6')
        {
            echo '"+ ('.$i2.' +1) month"';
        }
        if($var=='2')
        {
            echo '"+ ('.$i2.' +6) month"';
        }
        if($var=='1')
        {
            echo '"+ ('.$i2.' +12) month"';
        }


    }
 $chaine ="
 <table style='width:100%'>
 <tr>
 <th>Capital Principal</th>
 <th>Date d'&eacute;ch&eacute;ance</th>
 <th>Montant de l'&eacute;ch&eacute;ance</th>
 <th>Capital restant d&ucirc;</th>
 </tr>";
 $i2=0;
 $montant_echeance = round($montant_du / $nombre_echeances, 2);
 $reste=$montant_du ;
 while($i2 <= $nombre_echeances)
 {
     echo  $date =date("d-m-Y", strtotime($debut) .periode($pay_periodicity, $i2));             
        if ($i2 == $nombre_echeances)
        {
                $montnt_echeance = $reste;
                $reste = 0;
        }
     $chaine .= "<tr>
     <td>$montant_du</td>
      <td>".$date."</td>
     <td>$montant_echeance</td>
     <td>$reste</td></tr>'";
           $reste = $reste-$montant_echeance;
     $i2++;
        }
$chaine .="</table>";
print "$chaine";


    }

But instead of returning to me the good date, it returns to me:

"+ 0 month"11-10-2012"+ 1 month"11-10-2012"+ 2 month"11-10-2012"+ 3 month"11-10-2012"+ 4 month"11-10-2012"+ 5 month"11-10-2012"+ 6 month"11-10-2012"+ 7 month"11-10-2012"+ 8 month"11-10-2012"+ 9 month"11-10-2012"+ 10 month"11-10-2012"+ 11 month"11-10-2012"+ 12 month"11-10-2012"+ 13 month"11-10-2012"+ 14 month"11-10-2012"+ 15 month"11-10-2012"+ 16 month"11-10-2012"+ 17 month"11-10-2012"+ 18 month"11-10-2012"+ 19 month"11-10-2012"+ 20 month"11-10-2012"+ 21 month"11-10-2012"+ 22 month"11-10-2012"+ 23 month"11-10-2012"+ 24 month"11-10-2012"+ 25 month"11-10-2012"+ 26 month"11-10-2012"+ 27 month"11-10-2012"+ 28 month"11-10-2012"+ 29 month"11-10-2012"+ 30 month"11-10-2012"+ 31 month"11-10-2012"+ 32 month"11-10-2012"+ 33 month"11-10-2012"+ 34 month"11-10-2012"+ 35 month"11-10-2012"+ 36 month"11-10-2012"+ 37 month"11-10-2012"+ 38 month"11-10-2012"+ 39 month"11-10-2012"+ 40 month"11-10-2012"+ 41 month"11-10-2012"+ 42 month"11-10-2012"+ 43 month"11-10-2012"+ 44 month"11-10-2012"+ 45 month"11-10-2012"+ 46 month"11-10-2012"+ 47 month"11-10-2012"+ 48 month"11-10-2012"+ 49 month"11-10-2012"+ 50 month"11-10-2012"+ 51 month"11-10-2012"+ 52 month"11-10-2012"+ 53 month"11-10-2012"+ 54 month"11-10-2012"+ 55 month"11-10-2012"+ 56 month"11-10-2012"+ 57 month"11-10-2012"+ 58 month"11-10-2012"+ 59 month"11-10-2012"+ 60 month"11-10-2012 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Capital Principal   Date d'échéance     Montant de l'échéance   Capital restant dû
6210.04     11-10-2012  103.5   6210.04
6210.04     11-10-2012  103.5   6106.54
6210.04     11-10-2012  103.5   6003.04
6210.04     11-10-2012  103.5   5899.54
6210.04     11-10-2012  103.5   5796.04
6210.04     11-10-2012  103.5   5692.54
6210.04     11-10-2012  103.5   5589.04

It is really weird,

I'm a bit lost I really have no Idea of what is wrong with that function.

Any advice will be much appreciated.

Receive all my utmost Respect.

Kind Regards.

SP.

2
  • remove the echo before "$date =date("d-m-Y", strtotime($debut)... " Oct 11, 2012 at 6:56
  • Dear Sir I've done that, it still display the same things. Kind Regards; SP Oct 11, 2012 at 6:57

3 Answers 3

1

Your php code is not well written and it won't work. Since you didn't post the complete code, I don't know how to help. I gave some tips & ideas that will help you:

  1. periode() function should return the value, echo won't work
  2. modify your code as I mentioned
  3. Understand the strtotime() function and php

See my below sample code:

function periode($var, $i2)
{
    if($var=='1') {
        return '+'.$i2.' day';
    }
    else if($var=='2') {
        return '+'.$i2.' week';
    }
    else if($var=='3') {
        return '+'.$i2.' month';
    }
}

$current_date = date('Y-m-d');
echo date('Y-m-d', strtotime(periode(1, 1), strtotime($current_date)));
echo '<br />'.date('Y-m-d', strtotime(periode(2, 2), strtotime($current_date)));
echo '<br />'.date('Y-m-d', strtotime(periode(3, 1), strtotime($current_date)));
6
  • dear Sir, thanks for your quick reply, I've tried this $date =date("d-m-Y", strtotime(periode($pay_periodicity, $i2)), strtotime($debut)); it returns to me Warning: date() expects at most 2 parameters, 3 given Oct 11, 2012 at 7:22
  • Your periode() does not return valid parameters. echo '"+ '.$i2.' week"'; Above is not the correct one. Modify those as per my code. Refer this: php.net/manual/en/function.strtotime.php Oct 11, 2012 at 7:26
  • dear Sir, using hat modifications it returns to me 01-01-1970 for all dates Oct 11, 2012 at 7:31
  • It's wrong with '"+ '.$i2.' week"', etc,. Did you change that to valid parameters? Just analyze your code and you will be able make it work. Make sure you are passing valid date, and other parameters. Oct 11, 2012 at 7:35
  • Dear Sir, in some case I need to concatenate the $i2 + 2 Oct 11, 2012 at 7:48
1

remove the single quote(') in the following line <td>$reste</td></tr>'";

0
1

In the function periode, replace all the echos with return.

Also, I suggest using a switch statement instead of lots of ifs.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.