PHP-日期表单输入和不同格式

PHP-日期表单输入和不同格式,php,date,Php,Date,我有一个简单的web表单,它有一个带有HTML 5日期输入集的日期字段: <input type="date" id="dateOfBirth" name="dateOfBirth" placeholder="MM/DD/YYYY" required> 对于桌面浏览器或: 2020-03-20 用于移动设备等 我最终需要将结果以MM/DD/YYYY格式插入到我的数据库中,所以我要的是第一个版本。如何动态检测其到达的格式并将其转换为MM/DD/YYYY,而不考虑输入格式?Date

我有一个简单的web表单,它有一个带有HTML 5日期输入集的日期字段:

<input type="date"  id="dateOfBirth" name="dateOfBirth" placeholder="MM/DD/YYYY" required>
对于桌面浏览器或:

2020-03-20
用于移动设备等


我最终需要将结果以
MM/DD/YYYY
格式插入到我的数据库中,所以我要的是第一个版本。如何动态检测其到达的格式并将其转换为
MM/DD/YYYY
,而不考虑输入格式?

DateTime::format date\u create()和date\u format(),这将在插入数据库之前将日期重新格式化为所需格式。在成功时返回格式化的日期字符串,或在失败时返回假日期字符串

if(isset($_POST['dateOfBirth'])){
    $entered = $_POST['dateOfBirth'];
    $create = date_create($entered);
    // If user is using a browser that does not support HTML:5 input type date. 
    // check to make sure the entry is indeed in the proper format that supports date_format() 
    if($create !== false){
         $date = date_format($create, 'm-d-Y');
         // Insert into DB
    }else{
         // Handle failure error reporting
    }        
}
重要提示:正如El_Vanja
指出的,如果用户没有HTML5并且关闭了JS,没有正确的前端格式,就无法知道用户正在输入什么

您可以获取发布的日期、格式,然后将其发回,并让用户确认其输入是正确的

例如: 用户输入
03/05/2020
然后您可以在datetime中运行此命令,并显示为
date\u格式($create,'F-j-Y')返回
2020年3月5日
并让他们确认此条目,然后在确认后,将其作为您最初需要的格式插入数据库

也可以使用爆炸法进行验证

if(isset($_POST['dateOfBirth'])){
    $entered = $_POST['dateOfBirth'];
    $create = date_create($entered);

    // If user is using a browser that does not support HTML:5 input type date. 
    // check to make sure the entry is indeed in the proper format that supports date_format()    
    if($create !== false){
        $date = date_format($create, 'm-d-Y');
        // explode and create variables for each section of date 
        list($month, $day, $year) = explode("-", $date);
        // now you have each section of the datetime format in its own variable
        // validation can be passed through conditionals for $month and $day
        if($month <= 12){
            $returnDate = date_format($create, 'F-j-Y');

            // display this format to user and confirm
            // echo $returnDate and simple yes/no button form here 
            if(isset($_POST['yes'])){
                // set success css on input:focus display success to user
                // update DB

            }else{
                // set warning css on input:focus display warning to user
                // empty input form and have them reenter date.
            }
        }else{
            // handle $month error
        }
        // repeat conditional for $day
    }else{
        // $create === false --> handle error
    }
}
if(isset($\u POST['dateOfBirth'])){
$entered=$_POST['dateOfBirth'];
$create=日期\创建($entered);
//如果用户使用的浏览器不支持HTML:5输入类型日期。
//检查以确保条目的格式确实正确,支持日期\格式()
如果($create!==false){
$date=date_格式($create,'m-d-Y');
//分解并为日期的每个部分创建变量
列表($month,$day,$year)=分解(“-”,$date);
//现在,datetime格式的每个部分都有了自己的变量
//验证可以通过$month和$day的条件传递
如果($month)处理错误
}
}

但是,另一种方法是验证具有特定月份值项的选定字段。然后,您可以在后端使用返回的post变量进行格式化,并确保对日期/月份进行正确的格式化。

您所说的“通过”是什么意思?表单提交?日期输入始终在内部以“YYYY-MM-DD”格式存储日期值,无论显示格式如何-请参阅。您应该始终获得相同的格式。@El_Vanja我是指表单提交后的表单输入值-我从$\u POST['dateOfBirth'中获得的值]这很奇怪。我在我的机器上进行了测试,尽管显示是
mm/dd/yyyyy
,但提交的值是
yyy-mm-dd
。您是否对该值进行了任何其他修改?@El_Vanja没有对该值进行任何修改,只是调用
$\u POST['dateOfBirth']
要检索它,您始终可以使用
日期时间设置格式。但问题是-如果您从不支持该格式的浏览器中获取一个值,用户可以随意输入。您无法确定该日期是否为
mm/dd/yyyyy
日期。这并不完全是防错的。例如,您如何知道
03/04/2020
是3月4日还是4月3日?格式未知,因此您不能依靠创建
日期时间
对象来确保用户输入正确。@El_Vanja这是真的,本质上,如果不进行大量前端编码来错误检查客户的条目作为当天和/或当天的两个条目,几乎不可能判断出来月份可以是12个月或12个月。但是,您可以让用户通过更改格式以显示为
F/j/Y
,然后说
您的条目是2020年3月3日?
然后让用户确认。确认似乎是一个可行的解决办法。特别是如果您可以根据用户的区域设置对其进行格式化。
if(isset($_POST['dateOfBirth'])){
    $entered = $_POST['dateOfBirth'];
    $create = date_create($entered);

    // If user is using a browser that does not support HTML:5 input type date. 
    // check to make sure the entry is indeed in the proper format that supports date_format()    
    if($create !== false){
        $date = date_format($create, 'm-d-Y');
        // explode and create variables for each section of date 
        list($month, $day, $year) = explode("-", $date);
        // now you have each section of the datetime format in its own variable
        // validation can be passed through conditionals for $month and $day
        if($month <= 12){
            $returnDate = date_format($create, 'F-j-Y');

            // display this format to user and confirm
            // echo $returnDate and simple yes/no button form here 
            if(isset($_POST['yes'])){
                // set success css on input:focus display success to user
                // update DB

            }else{
                // set warning css on input:focus display warning to user
                // empty input form and have them reenter date.
            }
        }else{
            // handle $month error
        }
        // repeat conditional for $day
    }else{
        // $create === false --> handle error
    }
}