Files
Zinsrechner/index.php
2025-06-25 00:32:52 +02:00

217 lines
9.0 KiB
PHP

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zinsrechner</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #fff;
color: #333;
line-height: 1.6;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: 30px auto;
background: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
border: 1px solid #a81212;
}
h1 {
text-align: center;
color: #a81212;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
}
input[type="text"],
input[type="date"],
select {
width: calc(100% - 22px);
padding: 10px;
border: 1px solid #a81212;
border-radius: 4px;
box-sizing: border-box;
font-size: 16px;
}
input[type="submit"] {
background-color: #a81212;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
width: 100%;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #8f0e0e;
}
.result {
background-color: #ffe5e5;
border: 1px solid #a81212;
color: #a81212;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
font-size: 1.1em;
text-align: center;
}
.error {
background-color: #f8d7da;
border: 1px solid #f5c6cb;
color: #721c24;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
font-size: 1.1em;
text-align: center;
}
footer {
text-align: center;
margin-top: 40px;
font-size: 0.9em;
color: #777;
}
footer a {
color: #a81212;
text-decoration: none;
}
footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>Zinsrechner</h1>
<form action="" method="post">
<div class="form-group">
<label for="principal_amount">Kapital (Anlagebetrag):</label>
<input type="text" id="principal_amount" name="principal_amount" value="<?php echo isset($_POST['principal_amount']) ? htmlspecialchars($_POST['principal_amount']) : ''; ?>" placeholder="z.B. 1000">
</div>
<div class="form-group">
<label for="interest_rate">Zinssatz pro Jahr (%):</label>
<input type="text" id="interest_rate" name="interest_rate" value="<?php echo isset($_POST['interest_rate']) ? htmlspecialchars($_POST['interest_rate']) : ''; ?>" placeholder="z.B. 3.5">
</div>
<div class="form-group">
<label for="start_date">Startdatum:</label>
<input type="date" id="start_date" name="start_date" value="<?php echo isset($_POST['start_date']) ? htmlspecialchars($_POST['start_date']) : ''; ?>">
</div>
<div class="form-group">
<label for="end_date">Enddatum:</label>
<input type="date" id="end_date" name="end_date" value="<?php echo isset($_POST['end_date']) ? htmlspecialchars($_POST['end_date']) : ''; ?>">
</div>
<div class="form-group">
<label for="preset_years">Oder vordefinierte Jahre auswählen:</label>
<select id="preset_years" name="preset_years">
<option value="">Bitte auswählen</option>
<option value="1" <?php echo (isset($_POST['preset_years']) && $_POST['preset_years'] == 1) ? 'selected' : ''; ?>>1 Jahr</option>
<option value="2" <?php echo (isset($_POST['preset_years']) && $_POST['preset_years'] == 2) ? 'selected' : ''; ?>>2 Jahre</option>
<option value="3" <?php echo (isset($_POST['preset_years']) && $_POST['preset_years'] == 3) ? 'selected' : ''; ?>>3 Jahre</option>
<option value="4" <?php echo (isset($_POST['preset_years']) && $_POST['preset_years'] == 4) ? 'selected' : ''; ?>>4 Jahre</option>
<option value="5" <?php echo (isset($_POST['preset_years']) && $_POST['preset_years'] == 5) ? 'selected' : ''; ?>>5 Jahre</option>
<option value="10" <?php echo (isset($_POST['preset_years']) && $_POST['preset_years'] == 10) ? 'selected' : ''; ?>>10 Jahre</option>
</select>
</div>
<div class="form-group">
<input type="submit" name="calculate_interest" value="Zinsen berechnen">
</div>
</form>
<?php
$errors = [];
if (isset($_POST['calculate_interest'])) {
$principal_amount_input = trim($_POST['principal_amount']);
$interest_rate_input = trim($_POST['interest_rate']);
$start_date_input = trim($_POST['start_date']);
$end_date_input = trim($_POST['end_date']);
$preset_years = (int)$_POST['preset_years'];
// Validate principal amount
if (empty($principal_amount_input) || !is_numeric(str_replace(',', '.', $principal_amount_input))) {
$errors[] = "Bitte geben Sie einen gültigen Kapitalbetrag ein.";
} else {
$principal_amount = (float)str_replace(',', '.', $principal_amount_input);
if ($principal_amount <= 0) {
$errors[] = "Der Kapitalbetrag muss positiv sein.";
}
}
// Validate interest rate
if (empty($interest_rate_input) || !is_numeric(str_replace(',', '.', $interest_rate_input))) {
$errors[] = "Bitte geben Sie einen gültigen Zinssatz ein.";
} else {
$interest_rate = (float)str_replace(',', '.', $interest_rate_input);
if ($interest_rate < 0) {
$errors[] = "Der Zinssatz kann nicht negativ sein.";
}
}
// Validate dates or preset years
if (empty($start_date_input) || empty($end_date_input)) {
if ($preset_years === 0) {
$errors[] = "Bitte geben Sie entweder Start- und Enddatum an oder wählen Sie vordefinierte Jahre aus.";
} else {
$start_date = new DateTime();
$end_date = new DateTime();
$end_date->modify("+$preset_years years");
}
} else {
try {
$start_date = new DateTime($start_date_input);
$end_date = new DateTime($end_date_input);
if ($start_date >= $end_date) {
$errors[] = "Das Enddatum muss nach dem Startdatum liegen.";
}
} catch (Exception $e) {
$errors[] = "Ungültiges Datumsformat. Bitte verwenden Sie das Format JJJJ-MM-TT.";
}
}
if (empty($errors)) {
$interval = $start_date->diff($end_date);
$days = $interval->days;
// Simple interest calculation (adjust as needed for compound interest)
$annual_interest = ($principal_amount * $interest_rate) / 100;
$calculated_interest = ($annual_interest / 365) * $days;
$final_amount = $principal_amount + $calculated_interest;
echo '<div class="result">';
echo '<h2>Ergebnis:</h2>';
echo '<p>Kapital: <strong>' . number_format($principal_amount, 2, ',', '.') . ' €</strong></p>';
echo '<p>Zinssatz: <strong>' . number_format($interest_rate, 2, ',', '.') . ' %</strong></p>';
echo '<p>Anlagedauer: <strong>' . $days . ' Tage</strong></p>';
echo '<p>Berechnete Zinsen: <strong>' . number_format($calculated_interest, 2, ',', '.') . ' €</strong></p>';
echo '<p>Endbetrag: <strong>' . number_format($final_amount, 2, ',', '.') . ' €</strong></p>';
echo '</div>';
} else {
echo '<div class="error">';
foreach ($errors as $error) {
echo '<p>' . $error . '</p>';
}
echo '</div>';
}
}
?>
</div>
<footer>
&copy; <?php echo date("Y"); ?> <a href="https://wachtel-it.de" target="_blank">Philipp Wachtel</a>
</footer>
</body>
</html>