So I'm trying to make the code echo the date for the last saturday of february of the chosen year, but the method I attempted using completely overwrote the value of the string. I need the string to keep its value but still modify it into telling me the last saturday of february.
Heres the code:
´´´ <?php
$valdatum = date ("y-m-d H:i T", 0);
if(isset($_POST["valdatum"])) {
$valdatum=filter_input(INPUT_POST, 'valdatum', FILTER_SANITIZE_SPECIAL_CHARS);
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Tjejvasan</title>
<meta charset="utf-8">
</head>
<body>
<form method="POST">
<input type="date" name="valdatum" value="<?= $valdatum;?>">
<input type="submit" value="Skicka">
</form>
<?php
echo $valdatum;
echo "<br>";
$valdatum = new DateTime;
$valdatum->modify('last saturday of february');
$valdatum2 = $valdatum->format('Y-m-d');
echo $valdatum2;
?>
</body>
</html>
´´´
CodePudding user response:
You seem to have tied yourself in a few knots. I think this is what you need, although your description is not 100% clear:
if(isset($_POST["valdatum"])) {
//use the date value submitted from the form
$valdatum = $_POST["valdatum"];
}
else
{
//use today's date when nothing else was submitted
$valdatum = date("y-m-d");
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Tjejvasan</title>
<meta charset="utf-8">
</head>
<body>
<form method="POST">
<input type="date" name="valdatum" value="<?= $valdatum;?>">
<input type="submit" value="Skicka">
</form>
<?php
//change the date to the last saturday in February of the same year
$febDate = new DateTime($valdatum);
$febDate->modify('last saturday of february');
echo $febDate->format('Y-m-d');
echo "<br>";
?>
</body>
</html>