I created a program that allows u to type in the date and then when u click submit it should say whether the date u typed matches the CURRENT DATE (so the date updates itself) anyways, i am not so good at php and this is what i could make: whats the problem and can u demonstrate how to fix it.
<?php
session_start();
$EntryError="";
if (isset($_POST['submit'])){
$entrydate = "";
$errorOccured = false;
if (isset($_POST['tsmdate'])){
$entrydate = trim($_POST['tsmdate']);
if (strlen($entrydate) == 0){
$EntryError = "date is missing";
$errorOccured = true;
}
}
else{
$EntryError = "date is missing";
}
}
$presentDate=date('Y-m-d');
if($date==$presentDate)
{
echo "same date";
}
else{
echo "different date";
}
?>
<html>
<head>
</head>
<body>
<form name="dates" id="dates" method="POST" action="">
<table cellpadding="5" border="0" width="100%">
<tr>
<td colspan="3" align="center">
<h1> select dates </h1>
</td>
</tr>
<tr>
<td width="30%" align="right">
<label for="tsmdate">Entry date </label>
</td>
<td align="left">
<input type="date" name="tsmdate" id="tsmdate" required="required">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="dates">
</td>
</tr>
</table>
</form>
</body>
</html>
You are comparing dates outside of the isset($_POST['submit'])
condition. Try this :
<?php
session_start();
$EntryError="";
if (isset($_POST['submit'])){
$entrydate = "";
$errorOccured = false;
if (isset($_POST['tsmdate'])){
$entrydate = trim($_POST['tsmdate']);
if (strlen($entrydate) == 0){
$EntryError = "date is missing";
$errorOccured = true;
}
else{
$presentDate=date('Y-m-d');
if(strtotime($entrydate) == strtotime($presentDate))
{
echo "same date";
}
else{
echo "different date";
}
}
}
else{
$EntryError = "date is missing";
}
}
?>
Use this code.
<?php
session_start();
if(isset($_POST['submit'])){
if(isset($_POST['tsmdate'])){
$entrydate = trim($_POST['tsmdate']);
if($entrydate == date('Y-m-d')){
echo "Same Date";
}
else{
echo "Different Date";
}
}
else{
echo "Date is missing";
}
}
?>
<html>
<body>
<form name="dates" id="dates" method="POST" action="">
<table cellpadding="5" border="0" width="100%">
<tr>
<td colspan="3" align="center">
<h1> select dates </h1>
</td>
</tr>
<tr>
<td width="30%" align="right">
<label for="tsmdate">Entry date </label>
</td>
<td align="left">
<input type="date" name="tsmdate" id="tsmdate" required="required">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="dates">
</td>
</tr>
</table>
</form>
</body>
</html>
You are comparing the wrong variable. You declared the entered date as $entryDate and then compared $date with the $presentDate
if($date==$presentDate)
To fix the problem you just use the variable that you declared for the entered date
if($entryDate==$presentDate)
And that should fix it.