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条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)