add files of code before my time in the class
This commit is contained in:
86
Zweites Jahr/alt/validate_date.php
Executable file
86
Zweites Jahr/alt/validate_date.php
Executable file
@@ -0,0 +1,86 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php
|
||||
// validate_date.php
|
||||
|
||||
//ini_set("display_errors", "on");
|
||||
|
||||
// M A I N
|
||||
$date = "29.02.2400";
|
||||
if(isValidDate($date)) echo "<p>Datum $date ist <strong>gültig</strong></p>";
|
||||
else echo "<p>Datum $date ist <strong>ungültig</strong></p>";
|
||||
|
||||
|
||||
// Functions
|
||||
function isLeapYear(int $year): bool {
|
||||
return ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0);
|
||||
}
|
||||
|
||||
function isValidDate(string $date): bool {
|
||||
$date_parts = ["Tag","Monat", "Jahr"];
|
||||
$date_array = explode(".", $date);
|
||||
|
||||
$days_of_month = [31,28,31,30,31,30,31,31,30,31,30,31];
|
||||
if(isLeapYear($date_array[2])) {
|
||||
$days_of_month[1] = 29;
|
||||
}
|
||||
|
||||
/*
|
||||
// geht nicht:
|
||||
echo $date_array;
|
||||
|
||||
// Ausgabe des Arrays zum Debuggen
|
||||
echo "<pre>";
|
||||
print_r($date_array);
|
||||
echo "</pre>";
|
||||
|
||||
// Benutzerfreundliche Ausgabe
|
||||
echo "<p>Tag: $date_array[0]<br>";
|
||||
echo "Monat: $date_array[1]<br>";
|
||||
echo "Jahr: $date_array[2]</p>";
|
||||
|
||||
|
||||
for($i = 0; $i < sizeof($date_array); $i++) {
|
||||
echo "<p>$date_parts[$i]: $date_array[$i]</p>";
|
||||
}
|
||||
|
||||
|
||||
echo "<pre>";
|
||||
print_r($date_array_assoz);
|
||||
echo "</pre>";
|
||||
*/
|
||||
/*
|
||||
$date_array_assoz = array_combine($date_parts, $date_array);
|
||||
foreach($date_array_assoz as $datepartkey => $datepartvalue) {
|
||||
echo "<p>$datepartkey: $datepartvalue</p>";
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// Monat auf Gültigkeit prüfen
|
||||
if($date_array[1] < 1 || $date_array[1] > 12) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Tag auf Gültigkeit prüfen
|
||||
if($date_array[0] < 1 || $date_array[0] > $days_of_month[$date_array[1]-1]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Jahr auf Gültigkeit prüfen
|
||||
if($date_array[2] < 0 || $date_array[2] > 3000) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
?>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user