How do I debug my PHP validation function?

I am creating a web form for my job that is validated with PHP. However, when I test the page, I keep returning all error messages without submitting the form correctly when valid information is entered. Below is a small section of code (including HTML sections).

<?php
$date =""
$dateerror = ""
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["date"])) {
        $dateerror = "Date is required";
    } else {
        $date = test_input($_POST["date"]);
        $array = explode("/", $date);

        $day = $array[1];
        $month = $array[0];
        $year = $array[2];

        if (!checkdate($month, $day, $year)) {
            $dateerror = "Date mustbe in M/D/Y format";
        } else {
            date_default_timezone_set("America/Anchorage");
            $today = strtotime("now");
            if (strtotime($date)>=$today) {
                $date = test_input($_POST["date"]);
            } else {
                $dateerror = "Date is before present day";
            }
        }
    }

<input type="text" size="9" name="date" id="date" required title="Please enter current date"><?php echo $dateerror; ?><br>

      

Again, the PHP code simply returns "Date to Today" even when the date is current.

+3


source to share


2 answers


If you want to check a date in PHP, the best way to do it is to use a class DateTime

viz createFromFormat

.

This call will create an object DateTime

set to the specified date in the given format, or false

if it was an invalid date.

So for example:

<?php
$input = "05/08/2015";

$test = DateTime::createFromFormat('d/m/Y', $input);
if (!$test) {
    print "You entered an invalid date";
    die;
}

$now = new DateTime();
if ($test < $now) {
    print "Date is before present.";
    die;
}
?>

      



Just. No need for regex or input explosion etc; just one simple test. Then you can also use the variable $test

to handle the date once you have determined that it is correct as it is a standard DateTime object.

[EDIT] I added a bit of code to deal with using a class DateTime

to handle date mappings to give a "to date" error.

The important point here is that if you have a DateTime object, you need to compare it to another DateTime object; the older one strtotime()

creates a different type of date resource before DateTime

and you cannot use them together (at least not without converting between them all the time).

+3


source


Solution: use date("M/D/Y")

:

$today = strtotime(date("M/D/Y")); // 1432958400
$date = strtotime($_POST["date"]); // user input. 05-30-2015 will yield 1432958400
// the rest of your logic here

      



Here's a specific solution for the code:

<?php
$date =""
$dateerror = ""
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["date"])) {
        $dateerror = "Date is required";
    } else {
        $date = test_input($_POST["date"]);
        $array = explode("/", $date);

        $day = $array[1];
        $month = $array[0];
        $year = $array[2];

        if (!checkdate($month, $day, $year)) {
            $dateerror = "Date mustbe in M/D/Y format";
        } else {
            date_default_timezone_set("America/Anchorage");
            $today = strtotime(date("M/D/Y"));
            if (strtotime($date)>=$today) {
                $date = test_input($_POST["date"]);
            } else {
                $dateerror = "Date is before present day";
            }
        }
    }

<input type="text" size="9" name="date" id="date" required title="Please enter current date"><?php echo $dateerror; ?><br>

      

+1


source







All Articles