I have seen other questions asked about how to parse a date to the day of the week with the date hard coded into the code, but how would I go about doing the same from a form that posts to itself? Basically there are two dropdown (one for month and one for day) and a field to enter a year. I can't figure out how to take these three fields and put it into a single $date and then useecho date('l', strtotime($date));
to display it.
<?php
session_start();
if(!isset($_SESSION['month'])){
$_SESSION['month']= 01;
}
$month = $_SESSION['month'];
if(isset($_POST['month'])){
$month = $_POST['month'];
}
if(!isset($_SESSION['day'])){
$_SESSION['day']= 01;
}
$day = $_SESSION['day'];
if(isset($_POST['day'])){
$day = $_POST['day'];
}
if(!isset($_SESSION['year'])){
$_SESSION['year']= 0000;
}
$year = $_SESSION['year'];
if(isset($_POST['year'])){
$year = $_POST['year'];
}
if(!isset($_SESION['date'])){
$_SESSION['date'] = 'month'.'/'.'day'.'/'.'year';
}
$date = $_SESSION['date'];
if(isset($_POST['date'])){
$date= $_POST['date'];
}
if(!isset($_SESION['date'])){
$_SESSION['date'] = $month.$day.$year;
}
$date = $_SESSION['date'];
if(isset($_POST['date'])){
$date= $_POST['date'];
}
echo date('l', strtotime($date));
session_destroy();
?>
My form is here:
<body>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="POST">
<fieldset>
<legend>Date to Day Converter</legend>
Month:<br>
<select name="month">
<option value=01>January</option>
<option value=02>February</option>
<option value=03>March</option>
<option value=04>April</option>
<option value=05>May</option>
<option value=06>June</option>
<option value=07>July</option>
<option value=08>August</option>
<option value=09>September</option>
<option value=10>October</option>
<option value=11>November</option>
<option value=12>December</option>
</select>
<br>
Day:<br>
<select name="day">
<option value=01>01</option>
<option value=02>02</option>
<option value=03>03</option>
<option value=04>04</option>
<option value=05>05</option>
<option value=06>06</option>
<option value=07>07</option>
<option value=08>08</option>
<option value=09>09</option>
<option value=10>10</option>
<option value=11>11</option>
<option value=12>12</option>
<option value=13>13</option>
<option value=14>14</option>
<option value=15>15</option>
<option value=16>16</option>
<option value=17>17</option>
<option value=18>18</option>
<option value=19>19</option>
<option value=20>20</option>
<option value=21>21</option>
<option value=22>22</option>
<option value=23>23</option>
<option value=24>24</option>
<option value=25>25</option>
<option value=26>26</option>
<option value=27>27</option>
<option value=28>28</option>
<option value=29>29</option>
<option value=30>30</option>
<option value=31>31</option>
</select>
<br>
Year:<br>
<input type="text" name"year">
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
First of all you have a bug in your HTML. You are missing an =
in the following line of code:
<input type="text" name"year">
This means your year will never be submitted and default to 0;
Secondly, have a look at this part of your code:
if(!isset($_SESION['date'])){
$_SESSION['date'] = 'month'.'/'.'day'.'/'.'year';
}
$date = $_SESSION['date'];
if(isset($_POST['date'])){
$date= $_POST['date'];
}
if(!isset($_SESION['date'])){
$_SESSION['date'] = $month.$day.$year;
}
$date = $_SESSION['date'];
if(isset($_POST['date'])){
$date= $_POST['date'];
}
In your first if
you set the date in session to 'month/day/year'. You do not post any 'date' variable, so the second if
won't be executed. The third if
won't be executed either, since you just set the date
variable in your session. That fourth if
is just a copy of the second one, and still won't be executed for the same reason.
In the end your $date wil contain 'month/day/year', which php can't parse, so it defaults to the unix epoch, which happens to be on a Wednesday.
You should change the entire block of code I quoted above to something like this:
$date = $month.'/'.$day.'/'.$year;
$_SESSION['date'] = $date;
Now your script should work as expected