gpt4 book ai didi

php - 从 php 字符串到 mysql 日期

转载 作者:行者123 更新时间:2023-11-29 21:36:36 24 4
gpt4 key购买 nike

我想做的是将信用卡信息从表单插入到我的 mysql 数据库中。首先,表单上到期日期的输入格式为 MM/YY(带或不带空格),我尝试将 "20" 字符串连接到年份但我总是得到 "20 YY" 结果,mysql 无法将其识别为日期。

我想要获取适合格式的过期日期,以便稍后可以将其更改为 MySQL 格式。有什么想法吗?

这是我到目前为止的代码:

<?php 
$cc_number="";
$cc_expire="";
$cc_cvc="";
$cc_number=$_POST['number'];
$cc_expire=$_POST['expiry'];
$cc_cvc=$_POST['cvc'];
$cc_pid=$_SESSION['pid'];

/*edit the expire date*/
$pieces=explode("/", $cc_expire);
$expire_dd="01";
$expire_mm=$pieces[0];

$expire_yy="20".$pieces[1];
$expire_yy=trim($expire_yy, "\x00..\x1F");

//$cc_expire = $expire_yy.$expire_mm.$expire_dd;
//$cc_expire = date('Y-m-d', strtotime(str_replace('-', '/', $cc_expire)));
echo "expire_yy = ".$expire_yy;
//echo "cc_expire = ".$cc_expire;
if (isset($_POST["submit"])) {
if (empty($cc_number) || empty($cc_cvc) || empty($cc_expire)) {
echo "<p align='center'><font color='red'><br><br><br><br>All fields are mandatory.</font></p>";
}
else {
$cc_expire = date('Y-m-d', strtotime($cc_expire));
$sql = "INSERT INTO credit_card (credit_card_number, cvc, expiration_date, pid)
VALUES ('$cc_number', '$cc_cvc', '$cc_expire', $cc_pid)";
if ($dbconn->query($sql)) {
//echo "<script> window.location.assign('../card.php'); </script>";
}
else {
echo "<p align='center'><font color='red'><br><br><br><br>Something went wrong.</font></p>";
}
}
}
?>

我希望所有卡都获得 mySql DATE 格式,即 01-MM-YYYY。

最佳答案

strtotime() 应该适合你。在这里了解更多 http://www.w3schools.com/php/func_date_strtotime.asp

关于php - 从 php 字符串到 mysql 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34877372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com