检查日期是今天的日期或昨天的日期

时间:2017-10-14 09:29:27

标签: php date

如果所选日期为当前日期昨天的日期,我想在表格中插入数据。

html代码:

<input type="text" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="<?php echo date('d/m/Y'); ?>">

php code:

if(isset($_POST['submit'])){

    $report_date=date('Y-m-d',strtotime($_POST['reportdate']));
    $check_date=date('d',strtotime($_POST['reportdate']));

    if($check_date!=date('d') or $check_date!=date('d')-1){

        echo "<h2>You can only submit dcr of current date or yesterday.</h2>";
     }
     else{

     $client=$_POST['CLIENT_NAME'];
     //$cars_string = implode(', ', $_POST['cars']);
     $productname_string=implode(', ',$_POST['productname']);
     $remarks=$_POST['remark'];

     $sql="insert into d_c_r    (bm,date_time,client,promoted_product,remark) values ('".$_SESSION['id']."','$report_date',$client,'$productname_string','$remarks')";

     $res=mysql_query($sql) or die(mysql_error());
    }
}

1 个答案:

答案 0 :(得分:0)

  

工作代码已更新:

工作演示:http://phpfiddle.org/main/code/fxh8-gnkw

<html>
<body>

<form action="" method="post" name="myform">

<input type="date" id="datepicker1" name="reportdate" class="gui-input" placeholder="Select Date" required="Select date" value="">

<button type="submit" name="submit"> Submit</button>
</form>
</body>
</html>

<?php

if(isset($_POST['submit']) && !empty($_POST['reportdate'])){

      $today_date  = date('d-m-Y'); // you get current date
      $date_tday = new DateTime($today_date);
      $date_tday->modify('-1 day');
      $get_yesterday_date_from_current = $date_tday->format('d-m-Y'); // you get Yesterday date that from current date


      $reportdate = $_POST['reportdate']; // you get report date that user is selected
      $report_date_format = new DateTime($reportdate);
      echo $report_date = $report_date_format->format('d-m-Y'); //you get report date that user is selected in d-m-Y

      /*
     $date = new DateTime($report_date);
     $date->modify('-1 day');
     echo $get_yesterday_date = $date->format('d-m-Y'); // you get Yesterday date that user is selected
      */

    if($report_date !== $today_date && $report_date !== $get_yesterday_date_from_current){

        echo "<h2>You can only submit dcr of current Date or Yesterday.</h2>";
     }
     else{

     echo "Inser Your Data OR What You WAnt To Do HERE";

    }
}

?>