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');
    }
wobsoriano
  • 12,348
  • 24
  • 92
  • 162
  • Use `!empty()` check – jitendrapurohit Oct 29 '15 at 04:46
  • @JitendraPurohit, this will only check for empty input right? – wobsoriano Oct 29 '15 at 04: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. – Mike Oct 29 '15 at 04:50
  • 1
    Refer http://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format – ManiMuthuPandi Oct 29 '15 at 04: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'; }` – jitendrapurohit Oct 29 '15 at 04:54
  • Why use 1 text field for 2 dates anyway. Use 2 text fields, 1 for start date and 1 for end date, then use PHP checkdate() to validate. If you must use 1 text field you can still use checkdate() after you split the 2 dates. regex will validate the format but not the actual date. – CharlesEF Oct 29 '15 at 05:03

3 Answers3

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;
}
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
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()

Community
  • 1
  • 1
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
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.

Nilesh Chourasia
  • 408
  • 4
  • 11