30.05
This commit is contained in:
64
Tematy/T55/T55.4/index.php
Normal file
64
Tematy/T55/T55.4/index.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<!doctype html>
|
||||
<html lang="pl">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport"
|
||||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>T55.4</title>
|
||||
<link rel="stylesheet" href="../style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>Zadanie T55.4</h1>
|
||||
<h2>Autor: Jakub Grzegorczyk</h2>
|
||||
</header>
|
||||
<div class="box">
|
||||
<p>Napisz funkcję, która dla podanej daty w postaci mm, dd, YY sprawdzi, czy jest to prawidłowa data i jeśli tak sprawdzi, czy jest to data z przeszłości. Jeśli tak funkcja wypisze słowo „historia”, a jeśli nie wypisze „teraźniejszość lub przyszłość”. W przypadku błędnej danej funkcja kończy działanie i wyświetla komunikat o błędnej dacie.</p>
|
||||
</div>
|
||||
<div class="box">
|
||||
<?php
|
||||
function sprawdzDate($data) {
|
||||
$data = explode(" ", $data);
|
||||
if (count($data) != 3) {
|
||||
return "Błędny format daty. Użyj formatu mm dd YY.";
|
||||
}
|
||||
|
||||
list($mm, $dd, $yy) = $data;
|
||||
|
||||
if (!checkdate($mm, $dd, $yy)) {
|
||||
return "Błędna data.";
|
||||
}
|
||||
|
||||
$currentDate = new DateTime();
|
||||
$inputDate = strlen($yy) === 2
|
||||
? DateTime::createFromFormat('m d y', "$mm $dd $yy")
|
||||
: DateTime::createFromFormat('m d Y', "$mm $dd $yy");
|
||||
|
||||
if ($inputDate === false) {
|
||||
return "Błędna data.";
|
||||
}
|
||||
|
||||
if ($inputDate < $currentDate) {
|
||||
return "historia";
|
||||
} else {
|
||||
return "teraźniejszość lub przyszłość";
|
||||
}
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$data = htmlspecialchars($_POST['data']);
|
||||
$result = sprawdzDate($data);
|
||||
echo htmlspecialchars($result) . '<br>';
|
||||
echo '<button><a href="index.php">Powrót do formularza</a></button>';
|
||||
} else {
|
||||
echo '<form action="index.php" method="post">
|
||||
<label for="data">Wprowadź datę w formacie mm dd YY</label><br>
|
||||
<input type="text" name="data" id="data" placeholder="mm dd YY" required><br>
|
||||
<button type="submit">Sprawdź datę</button>
|
||||
</form>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user