0

How to validate user if input end date is more than 3 days from the input start date?

Given: PHP Code

if(isset($_POST['btnRequest']))
{
$leaveFrom = $_POST['leaveFrom'];
$leaveTo = $_POST['leaveTo'];
}
$sql_insert = "INSERT INTO leaves (leaveFrom, leaveTo) VALUES (?, ?)";



HTML Code
<input type="date" id="leaveFrom" name="leaveFrom" class="form-control" required="true" />
<input type="date" id="leaveTo" name="leaveTo" class="form-control"  required="true" />

Data types are 'date' from Microsoft SQL

Goal: To submit date if date ranges to 3 days or less.

(This is for a user who can file his/her 'Sick Leave' to the admin with a limit of 3 days of leave)

I tried Zain Farooq's code but here's what I tried.

    function fourDays()
    {
        return date("Y-m-d"),strtotime("+ 4days")); 
    }

    $date = date('Y-m-d'),strtotime($leaveTo)); // I want to 
    validate the input from the input 'leaveFrom' but I have no idea how

    if($date >= fourdays())
    {
        $dispMsg = $dateerrorMsg;
    }
jmnl
  • 21
  • 2
  • 3
    Your question is both unclear and too broad. Can you post something that you tried and it may have failed you? That way it would have at least shown some effort on your part. Remember, we're always glad to help those who first help themselves. – Funk Forty Niner Mar 01 '18 at 12:54
  • And what format is a date in when its _Data types are 'date' from Microsoft SQL_ – RiggsFolly Mar 01 '18 at 12:59
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Michel Mar 01 '18 at 13:18
  • @FunkFortyNiner Please see edited version. I couldn't find an answer for 2 months already. :( – jmnl Mar 01 '18 at 14:32
  • There is syntax error in your function – Zain Farooq Mar 01 '18 at 14:35

2 Answers2

1

Use this code to compare two dates

function today()// it will get date of today
{
    return date("Y-m-d");

}
function last_three_day()//it will get date of last third day
{
    return date("Y-m-d",strtotime("-3 days"));

}


$date = date('Y-m-d',strtotime('2018-03-01')); //it is your date which you want to compare


if($date >= last_three_day() && $date <= today())
{
    echo "yes";
}


Update
Actually you were missing some logic. You were compare two wrong dates. You must have to make difference between user input dates then you will be able to get true results

<?php

if(isset($_POST['submit']))
{
    $leaveFrom = date_create($_POST['leaveFrom']);
    $leaveTo =  date_create($_POST['leaveTo']);
    $dayDifference = date_diff($leaveFrom, $leaveTo)->format('%d');
    $yearDifference = date_diff($leaveFrom, $leaveTo)->format('%y');
    $monthDifference = date_diff($leaveFrom, $leaveTo)->format('%m');

    /*These conditions will check the year and month differences too.*/
    if($yearDifference==0)
    {
        if($monthDifference==0)
        {
            if($dayDifference<=4 && $dayDifference>0)
            {
                echo "Eligible";
            }
            else//else for day difference
            {
                echo "Not Eligible";
            }
        }
        else
        {
            echo "Not Eligible";

        }//else for month difference
    }
    else
    {
        echo "Not Eligible";

    }//else for year difference
}

?>
<html>
<body>
<form action="" method="post">
    <label for="leaveFrom">Leave From</label> <input type="date" name="leaveFrom"><br>
    <label for="leaveTo">Leave To</label><input type="date" name="leaveTo"><br>

<br>    <input type="submit" name="submit">
</form>
</body>
</html>
Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
  • Thank you so much for this answer! It helped me with the logic yet I have 2 inputs and tried your code but it won't work. function fourDays() { return date("Y-m-d"),strtotime("+ 4days")); } $date = date('Y-m-d'),strtotime($leaveTo)); // I want to validate the input from the input 'leaveFrom' but I have no idea how if($date >= fourdays()) { $dispMsg = $dateerrorMsg; } – jmnl Mar 01 '18 at 14:28
  • Shouldn't it strtotime("- 4days")) be in your `fourDays()` method? – Zain Farooq Mar 01 '18 at 14:31
  • function fourDays() { return date("Y-m-d",strtotime("- 4days")); } //for last forth day date – Zain Farooq Mar 01 '18 at 14:33
  • There was problem in your function – Zain Farooq Mar 01 '18 at 14:34
  • The logic is 'If end date is 4days or more from the input start date -> error'. But I didn't put the logic of start date since it's an input. – jmnl Mar 01 '18 at 14:37
  • 4 days in terms of future? If yes then its correct. But you have to put `-4` in terms of past. Anyway I have updated my answer – Zain Farooq Mar 01 '18 at 14:41
  • check inbox. I will sure help you in php – Zain Farooq Mar 01 '18 at 15:10
0

Found the solution. Big thanks to Zain Farooq!

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


    $leaveReason = $_POST['leaveReason'];
    $leaveFileDate = $_POST['leaveFileDate'];
    $leaveFrom = date_create($_POST['leaveFrom']);
    $leaveTo =  date_create($_POST['leaveTo']);


$dayDifference = date_diff($leaveFrom, $leaveTo)->format('%d');

if($dayDifference<=4)
{
    $sql_insert = "INSERT INTO leaves (leaveReason, leaveFileDate, leaveFrom, leaveTo, leaveStatus, accountID, ltypeID) 
                   VALUES(?, ?, ?, ?, ?, ?, ?)";
    $params_insert = array($leaveReason, $leaveFileDate, $leaveFrom, $leaveTo, 'Pending for Approval', $accID, $ltypeID);


    $stmt_insert = sqlsrv_query($con, $sql_insert, $params_insert);

    if($stmt_insert === false) {
        #die(print_r(sqlsrv_errors(), true));
        $dispMsg = $errorMsg;
    }
    else 
    {
        $dispMsg = $successMsg;
    }
}
else
{
    $dispMsg = $dateerrorMsg;
}
jmnl
  • 21
  • 2