0

I'm adding date of birth validation to my Twilio flow. Format is mm/dd/yyyy. So user would input 01021999 for Date of Birth: 01-02-1999.

I pass the input as a parameter to my validation script (PHP) on my VPS via and http request. The problem is that if I manually set the $dob variable in my script it works, but if I pull that info from twilio there's an issue and the http request sends an error.

I know php treats numbers leading with zeros different and you have to pass them as strings. Tried using strval() to the dob variable to be able to use the input but haven't had any luck.

Works:

$account_number = 1234;
$dob = "01021999";
$dob_length = strlen($dob);
if ($dob_length = 8) {
        echo $dob_month = substr($dob, 0, 2);
        echo $dob_day = substr($dob, 2,2);
        echo $dob_year = substr($dob, 4, 4);
        echo $dob_full = $dob_month . "-" . $dob_day . "-" . $dob_year;
        $sql1 = "SELECT * FROM accounts WHERE Acct_Nbr = '".$account_number."' AND Guar_DOB LIKE '%".$dob_full."%'  ";
        $rows = getRows($sql1);

Doesn't work (with or without turning the $dob to a string using strval() :

require_once('logs.php');
require_once('db.php');
require_once('rest.php');

$data = $_REQUEST;
start_log();
$filename = basename(__FILE__);
echo "<pre>".print_r($data,true)."</pre>";
end_log();

header("Content-Type: application/json; charset=UTF-8");
$rfields = explode(",","client_id,account_number,dob");

foreach($rfields as $rf){
        if(!isset($data[$rf])){
                $message = $rf." is required.";
                $status = "error";
                echo json_encode(compact('status','message')); die();
        }
}

extract($data);
$dob_str = strval($dob);
$dob_length = strlen($dob_string);
if ($dob_length = 8) {
        echo $dob_month = substr($dob_str, 0, 2);
        echo $dob_day = substr($dob_str, 2,2);
        echo $dob_year = substr($dob_str, 4, 4);
        echo $dob_full = $dob_month . "-" . $dob_day . "-" . $dob_year;
        $sql1 = "SELECT * FROM accounts WHERE Acct_Nbr = '".$account_number."' AND Guar_DOB LIKE '%".$dob_full."%'  ";
        $rows = getRows($sql1);

}
UzZzi
  • 55
  • 1
  • 7
  • Have you already tried to use Carbon to manage the dates? https://carbon.nesbot.com/docs/#api-instantiation – WILLIAM DAZA Sep 24 '21 at 21:23
  • $orgDate = "2021-09-24"; $newDate = date("d-m-Y", strtotime($orgDate)); Now the date format is: d-m-Y [here are all date formats](https://www.php.net/manual/en/datetime.format.php) – WILLIAM DAZA Sep 24 '21 at 21:26
  • @WILLIAMDAZA Seems like a useful tool. I'm not looking to change the format just grabbing user input through DTMF, adding "-" so it looks more like a date, and using that date to search through a database to see if it matches with the record. – UzZzi Sep 24 '21 at 21:37

1 Answers1

0

Try this

        $dob = '01021999';
        $account_number = 'whatever';
        if (validateDate((string)$dob, 'dmY')) {
            $date = DateTime::createFromFormat('dmY', $dob);
            $final_date = $date->format('Y-m-d');
            $sql1 = "SELECT * FROM accounts WHERE Acct_Nbr = '" . $account_number . "' AND Guar_DOB LIKE '%" . $final_date . "%'  ";
            $rows = getRows($sql1);
        }


        function validateDate($date, $format = 'Y-m-d H:i:s')
        {
            $d = DateTime::createFromFormat($format, $date);
            return $d && $d->format($format) == $date;
        }

WILLIAM DAZA
  • 118
  • 1
  • 7