Calculating dates

I have a simple loan calculator that I have developed. You put in a start date and a due date along with interest and principal. The calculator will tell you the start and due dates, interest rate, principle amount, with simple interest due and total amount required to repay. For the most part the calculator works. But when I put in a year date larger than 2037 the code will not work. I think I am missing something. If anyone has any suggestions please let me know.

Thanks,
r

Here is my code:
[php]

<?php $interestdue = 0.00; $startdate =""; $newdate = ""; $enddate =""; echo ""; if(isset($_POST['month']) && isset($_POST['endmonth'])) { if(isset($_POST['year']) && !empty($_POST['year']) && isset($_POST['endyear']) && !empty($_POST['endyear'])){ //start month, day and year of loan $month = $_POST['month']; $day= $_POST['day']; $year = $_POST['year']; //end month, day and year of loan $endmonth = $_POST['endmonth']; $endday= $_POST['endday']; $endyear = $_POST['endyear']; //start date of loan $startdate = mktime(0, 0, 0, $month, $day, $year); //end date of loan $enddate = mktime(0, 0, 0, $endmonth, $endday, $endyear); if($startdate < $enddate){ //print out the start and due dates echo ""; echo ""; echo ""; if(isset($_POST['interest']) && !empty($_POST['interest'])) { $interest = $_POST['interest']; //convert interest into decimal $inter = $interest / 100; echo ""; if(isset($_POST['principle']) && !empty($_POST['principle'])) { $principle = $_POST['principle']; //principle amount of the loan in currency format echo ""; //interest due for the loan $interestdue = $principle * $inter * $newdate; echo ""; echo ""; } echo "
Simple Interest Loan
Start Date: "; echo date("F j, Y", $startdate); echo "
Due Date: "; echo date("F j, Y", $enddate); //work out the dates differences $dates = $enddate - $startdate; $newdate = floor($dates / (365 *24 * 60 * 60)); //echo $newdate; }else{ echo "Please make sure start date is less than end date"; } }else{ echo "

Please check years


"; } echo "
Interest Rate: "; echo $inter."%"; }else{ echo "

Please add the interest


"; } echo "
Principle Amount: "; echo "$".number_format($principle, 2, '.',','); echo "
Simple Interest Due: "; echo "$".number_format($interestdue, 2, '.',','); echo "
"; //total amount of loan to pay back echo "The amount required to
"; echo "repay the loan is: "; echo "
"; echo "$".number_format($principle + $interestdue, 2,'.',','); }else{ echo "

Please add the principle


"; } echo "
"; ?>

[/php]

I believe DateTime will give you more accurate results?

[php]<?php
date_default_timezone_set(‘America/Detroit’); // Set the Default Time Zone:

$month = ‘Aug’;
$day = 28;
$year = 2015;

$endMonth = ‘Jan’;
$endDay = 1;
$endYear = 2018;

$startDate = new DateTime($month . ’ ’ . $day . ', ’ . $year);
$endDate = new DateTime($endMonth . ’ ’ . $endDay . ', ’ . $endYear);

echo 'Start Date : ’ . $startDate->format(“F j, Y”) . “
\n”;
echo 'End Date : ’ . $endDate->format(‘F j, Y’) . “
\n”;

$difference = $endDate->diff($startDate);

echo ‘

’ . print_r($difference, 1) . ‘
’;[/php]

Strider64:

Thank you for your reply. Your code works but when I plug it into my code i get an Fatal error: Uncaught exception ‘Exception’ with message ‘DateTime::__construct(): Failed to parse time string (01 01 2015) at position 0 (0): Unexpected character’. Would there be something in the form I would need to change? Thank you for your time.

Thanks,
r.

The $difference (I trying to find the right word, so it might be wrong) is an instance which is made up a bunch of objects.

For example
[php]$difference->days[/php]

would give you the number of days till the $endate.

Rereading your problem, I think the dates that your are pulling from the form are not strings…brb with a new reply.

[php]<?php

$_POST[‘month’] = 8;
$_POST[‘day’] = 28;
$_POST[‘year’] = 2015;

$startDate = new DateTime();

$startDate->setDate($_POST[‘year’], $_POST[‘month’], $_POST[‘day’]);

echo ‘

’ . print_r($startDate, 1) . ‘
’;[/php]

Strider64:

The problem was in my form. I had to change some of the coding. The dates do come up. The thing I am running into now is I need to some how find the time in years so I can place it into the formula to get the interest due. In my old code I did this:
[php]
//work out the dates differences
$dates = $enddate - $startdate
$newdate = floor($dates / (365 24 * 60 * 60));
[/php]
I took the difference in dates and divided by (365 * 24
60*60). With the new datetime I am not sure how to do the $newdate so I can put it into the interest due formula like this:

[php]
//interest due for the loan
$interestdue = $principle * $inter * $newdate;
[/php]

I am not sure how to get the formula for datetime. I appreciate all your help.
Thanks,
r.

Sponsor our Newsletter | Privacy Policy | Terms of Service