BGamlin Posted September 26, 2011 Share Posted September 26, 2011 Hi, Firstly, I hope this is the correct area for this post. I am basically developing a piece of PHP code that will calculate when money is due in from a customer as part of an installment paying system. This should be fairly simple what I am trying to accomplish but I always seem to have trouble with dates in PHP! Please see below code. for($count = 1; $count <= $_POST['installments'] { $insStart = strtotime("+4 month", mktime($insStart)); echo "<tr>"; echo "<td style='text-align:center;'>$count</td>"; echo "<td><input style='border:none; text-align:center' width:100%;' type='text' name='installmentDate[]' value='".date("d/m/Y", $insStart)."' /></td>"; echo "<td><input style='border:none; text-align:right' width:100%;' type='text' name='installmentValue[]' value='".number_format($installmentBase,2)."' /></td>"; echo "</tr>"; $insStart = date("d/m/Y", $insStart); $count++; } The above is originally getting the value $insStart from a variable earlier on in the code which is assigned from a database table. It is then taking that plain text formatted date (d/m/Y) and converting it to a UNIX stamp to eventually use the strtotime command to add on the designated time (in this case 4 months). The new date is then re-assigned at the bottom of the loop to be used again until the loop is complete. In its current state the first entry of the loop (where the code picks up the table entry) seems to work correctly. However, for the other results the data stays the same as that of the first and no matter what I do I can't make them change. Please Help! Many Thanks, Ben Quote Link to comment Share on other sites More sharing options...
dougjohnson Posted September 26, 2011 Share Posted September 26, 2011 I think you need to increment $intStart inside of the loop. "+4 month" doesn't increment the $inStart variable. Quote Link to comment Share on other sites More sharing options...
BGamlin Posted September 27, 2011 Author Share Posted September 27, 2011 Hey Doug, Thanks for your reply. I have since managed to solve the problem. It has used a bit more code but does the job nicely For anyone else who is having similar problems as me I have attached the code below. for($count = 1; $count <= $_POST['installments'] { list($d, $m, $y) = split("/", $insStart); $unix = mktime(0,0,0,$m,$d,$y); if($_POST['instFreq'] == "month") { $end_date = date("d/m/Y",strtotime("+1 months",$unix)); } if($_POST['instFreq'] == "quaterly") { $end_date = date("d/m/Y",strtotime("+4 months",$unix)); } if($_POST['instFreq'] == "biAnnual") { $end_date = date("d/m/Y",strtotime("+6 months",$unix)); } if($_POST['instFreq'] == "annual") { $end_date = date("d/m/Y",strtotime("+12 months",$unix)); } echo "<tr>"; echo "<td style='text-align:center;'>$count</td>"; echo "<td><input style='border:none; text-align:center' width:100%;' type='text' name='installmentDate[]' value='".$end_date."' /></td>"; echo "<td><input style='border:none; text-align:right' width:100%;' type='text' name='installmentValue[]' value='".number_format($installmentBase,2)."' /></td>"; echo "</tr>"; $insStart = $end_date; $count++; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.