0

I have a simple code that will split the ranged dates into two. Here it is:

html:

<input type="text" name="range" />
<button type="submit>Submit</button>

php

$range = $_POST['range'];
$reservation = explode("-", $range);
$from        = date('Y-m-d', strtotime($reservation[0]));
$to          = date('Y-m-d', strtotime($reservation[1]));

The splitting of the date works, but whenever I put any character in the textbox and click submit, I get an undefined offset: 1 because I don't have any validation for the range dates.

Is there a way to validate ranged dates like for example if I put "10/29/2015 - 10/31/2015" in the textbox and click submit, I will get "success" and if I put any character like "asdasdasda", I will get "failed".

Any help would be much appreciated.

UPDATE

The validation if the input are in ranged date is now working, but the date validation is not. I'm using checkdate function

    $reservation = explode("-", $_POST['range']);

    if (isset($reservation[0]) && isset($reservation[1])) {
        $from = date('Y-m-d', strtotime($reservation[0]));
        $to   = date('Y-m-d', strtotime($reservation[1]));

        $checkFrom = explode('-', $from);
        $checkTo   = explode('-', $to);

        if (!checkdate($checkFrom[1], $checkFrom[2], $checkFrom[0]) || !checkdate($checkTo[1], $checkTo[2], $checkTo[0])) {
            die('invaild date');
        }
    } else {
        die('invaild date');
    }
6
  • Use !empty() check Commented Oct 29, 2015 at 4:46
  • @JitendraPurohit, this will only check for empty input right? Commented Oct 29, 2015 at 4:46
  • You need to do something like if (!isset($reservation[1])) to make sure the explode worked. Then check if ($from === false || $to === false) to make sure they contain valid dates. Commented Oct 29, 2015 at 4:50
  • 1
    Refer stackoverflow.com/questions/19271381/… Commented Oct 29, 2015 at 4:52
  • or use regex if(preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}\-[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/", $_POST["range"]) === 0) { echo 'error'; } Commented Oct 29, 2015 at 4:54

3 Answers 3

1

Your explode() should be based on delimiter used in form

$range = $_POST['range'];
$reservation = explode("-", $range);
if(count($reservation) == 2) {
    if(validateDate($reservation[0]) && validateDate($reservation[1])){
        $from        = date('Y-m-d', strtotime($reservation[0]));
        $to          = date('Y-m-d', strtotime($reservation[1]));
    } else {
        //error
    }

} else {
     //error
}

function was copied from this answer or php.net

If you want you can even use date_parse()

Sign up to request clarification or add additional context in comments.

Comments

1

Try this regex to validate the date range -

if (preg_match("/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}\-[0-9]{1,2}\/[0-9]{1,2}\/[0-9‌​]{4}/", $_POST["range"]) === 0) { 
  echo 'error';  //or just return;
}

Comments

0

You can do one thing make the field is readonly and apply required validation on that field, so if the field is readonly then user can only able to select date range which is applied on this field and user can not able to do any input manually and if user will not choose this then your required validation will show the required message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.