使用来自API调用的数据验证出生日期



我将出生日期验证添加到我的Twilio流。格式为mm/dd/yyyy。因此,用户将输入01021999作为出生日期:01-02-1999。

我通过http请求将输入作为参数传递给VPS上的验证脚本(PHP)。问题是,如果我手动设置$dob变量在我的脚本工作,但如果我从twilio拉的信息有一个问题,http请求发送一个错误。

我知道php对待以0开头的数字不一样,你必须把它们作为字符串传递。尝试对dob变量使用strval()以能够使用输入,但没有任何运气。

:

$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);

不起作用(无论是否使用strval()将$dob转换为字符串):

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);
}

试试这个

$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;
}

最新更新