如果日期晚于今天的日期,如何不将用户添加到数据库?

时间:2017-02-09 20:35:28

标签: javascript date date-comparison

我这里有点问题。我是PHP和JS的新手,所以。我有一个PHP代码,里面有MySQL数据库。在JS文件中,我编写了代码,用于检查用户输入的日期是否早于今天的日期。现在它将所有用户添加到数据库中,但我只需要添加早于今天日期输入的用户(仅添加具有TRUE if语句的用户)。我该怎么办?谢谢你的帮助。

以下是我的JS代码部分:

if (x.getTime() < today.getTime()) {
    alert ("Employees has been succesfully added to the database");
}
else
{
    alert ("You have entered date which is later than todays date. Re-enter it");
}
}

这是我的PHP代码:

<?php
try {
$db = new PDO( 'mysql:host=127.0.0.1;dbname=employees;charset=utf8','root','' );
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (isset($_POST['Submit'])) {
$name = $_POST['name'];
$surname = $_POST['surname'];
$employmentDate = $_POST['employmentDate'];

$insert = $db->prepare("INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)");

$insert->bindParam(':name',$name);
$insert->bindParam(':surname',$surname);
$insert->bindParam(':employmentDate',$employmentDate);
$insert->execute();
}
}
catch (PDOException $e) {
echo "There is something wrong with the database".$e->getMessage();
die();
}
?>

1 个答案:

答案 0 :(得分:0)

var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
    alert("Today is before January 14, 2100.");
return;
 }
   ...Database insert Code here

编辑回答你的问题我相信。如果您在正确的范围块中,则只需要调用数据库添加。

查看W3学校: http://www.w3schools.com/js/js_date_methods.asp

如果您想让生活更轻松,请使用moment.js https://momentjs.com/