suresh
suresh

Reputation: 439

compare two dates and second date should be greater than first date in php

Hi guys i am trying to compare two dates from date and to date. TO Date shoudl be greater than from date. I didn't get any idea of how to do validation for that in php. Can anyone help me how can we do that.

Here is my code:

<form method="post" action="downloadreport.php">

                            <div class="col-md-12" >
                                    <div class="form-group label-floating">
                                        <label class="control-label">From  Date</label>
                                        <input type="text" class="form-control" id="datepicker" name="datepicker" > 
                                    </div>
                                </div>
                            <div class="col-md-12">
                                    <div class="form-group label-floating">
                                        <label class="control-label">To Date</label>
                                        <input type="text" class="form-control" id="datepicker1" name="datepicker1" > 
                                    </div>
                                </div>
                        <div class="col-md-12">
                                <div class="form-group label-floating">
                            <button type="submit" name="submit" value="submit" class="btn btn-info form-control">Download Report</button>
                                </div>
                       </div>
                        </form>

Downalodreport.php

    <?php
error_reporting(E_ALL ^ E_NOTICE);
if (isset($_POST['submit'])) {
    $fromdate =$_POST['datepicker'];
    $todate = $_POST['datepicker1'];
    $usertype= $_POST['trackforums'];


    //my code will start
}

Can anyone help me how to do that

Thanks in advance.

Upvotes: 1

Views: 1284

Answers (2)

MorganFreeFarm
MorganFreeFarm

Reputation: 3733

Here:

<?php

    $fromdate = strtotime($_POST['datepicker']);
    $todate = strtotime($_POST['datepicker1']);

    if ($todate > $fromdate ) {
        echo 'Todate is higher!';
    } else {
        echo 'Fromdate is higher!';
    }

    ?>

U can learn more about strtotime function here

Upvotes: 1

maalls
maalls

Reputation: 759

If both date has a standard format, you can use the strtotime function to compare them. It will convert the date into a unix timestamp (the number of seconds since January 1 1970).

Upvotes: 1

Related Questions