日期/时间条件

时间:2015-05-03 20:23:12

标签: php mysql date time

我有这个代码,我想只在请求的操作是在特定日期进行时才执行sql查询。 (例如,如果我将日期设置为4.05.2015,则查询将仅在该日进行,如果我尝试在5.05.2015进行查询,则不会发生任何事情。 Thx in advice

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);

3 个答案:

答案 0 :(得分:3)

$date = "2015-05-05";
$today = date('Y-m-d');

if ($date == $today){

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);

}

答案 1 :(得分:1)

您需要将代码包装在一个实际检查提交日期是否有效的语句中:

$check_date = (some date as a string);
$check_date = date('Y-m-d', strtotime($check_date));
$current_date = date('Y-m-d');

if ($check_date == $current_date) {
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);
}

请记住,此示例假定$ check_date最初是一个字符串。

答案 2 :(得分:0)

使用PDO。然后将日期检查为数字,LPB1l建议。

$box = $_GET['box3'];
$number = $_GET['nr'];

try {
    $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
    $conn->exec("SET CHARACTER SET utf8");  
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "
    UPDATE `nume`   
    SET `venit3` = :box,    
    WHERE `nr` = :number
    "; 

    $statement = $conn->prepare($sql);

    $statement->bindValue(":box", $box);
    $statement->bindValue(":number", $number);   

    $count = $statement->execute();    

}
    catch(PDOException $e) {
    echo $e->getMessage();
}