简体   繁体   English

PHP服务器端对将来日期与今天日期的比较进行验证

[英]PHP server side validation of future date in comparison with today's date

My database has a column, for example, named 我的数据库有一个列,例如,名为

expected_event_date data type of which is set to timestamp and it's attribute is CURRENT_TIMESTAMP expected_event_date数据类型,其数据类型设置为timestamp ,其属性为CURRENT_TIMESTAMP

Users input expected future event date through a form. 用户通过表格输入预期的未来事件日期。

How shall I validate the future date in comparison with current date in this case? 在这种情况下,与当前日期相比,我该如何验证未来日期? The server should omit an error in case users input past date. 如果用户输入过去的日期,则服务器应忽略错误。

I'm looking for something like, 我正在寻找类似的东西,

$error = "invalid date" 
if ($_POST['expected_event_date'] < today's date only but not time ) {
echo $error;    
}

Any idea? 任何想法?

Here is a function for you :) 这是给你的功能:)

$error = "invalid date" 
if ($this->isValidDate($_POST['expected_event_date'],'Y-m-d')) {
echo $error;    
}

public function isValidDate($date,$dateFormat){

    $date = trim($date);
    $time = strtotime($date);

    if(date($dateFormat, $time) < date('Y-m-d')){
        return true;
    }
    else {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM