Jump to content

PHP Dates


BGamlin

Recommended Posts

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

Link to comment
Share on other sites

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++;
				}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.