doukenqiong0588 2017-01-04 18:30
浏览 41
已采纳

php / html:检查键入的日期是否与当前日期匹配

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>
  • 写回答

3条回答 默认 最新

  • doumei1926 2017-01-04 18:38
    关注

    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";
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?