在数据库中检查当前日期与日期

时间:2011-12-31 04:39:55

标签: php time

我正在使用此代码检查数据库是否有结束编辑的日期(说今天的日期是12/30/11最后编辑日期是或是12/12/10 =已锁定或今天日期为12 / 30/11编辑的最后日期是或是12/12/13 = UNLOCKED&转发到编辑网站)

所以考虑到这一点就是问题所在:代码我总是说你的账户无论锁定日期都被锁定了,而且我找不到解决方案:(。

顺便提一下,请记住此时已经发送了标题。

<?php
$id = $_GET['id'];
// Define MySQL Information.
$mysqlhost="***************"; // Host name of MySQL server.
$mysqlusername="**********"; // Username of MySQL database. 
$mysqlpassword="*********"; // Password of the above MySQL username.
$mysqldatabase="*************"; // Name of database where the table resides.
// Connect to MySQL.
mysql_connect("$mysqlhost", "$mysqlusername", "$mysqlpassword")or die("Could not connect to     MySQL.");
mysql_select_db("$mysqldatabase")or die("Could not connect to selected MySQL database.");

$infosql = "SELECT * FROM premiersounds_users WHERE customer_id = $id";
$inforesult = mysql_query($infosql) or die(mysql_error());
$info = mysql_fetch_array($inforesult);


$l_date=$info['lockout_date'];

//Get current date from server
    $format="%m/%d/%y";
$c_date=strftime($format);
//set sessions
$_SESSION['current_date'] = $c_date;
$_SESSION['lockout_date'] = $l_date;

//Check is Current date = lockout date
if ($c_date <= $l_date) { header("location:/planner_scripts/documnet_editors    /edit_weddingplanner.php?id=$id"); } else {echo 'Whoops! Were sorry your account has been locked to edits because your event is less than 48 hours from now or your event has passed. To make changes to your event please contact your DJ.'; echo'<br/>'; echo ' Todays Date: ';echo $c_date; echo ','; echo ' Last Date for edits: '; echo $l_date;}
?>
<?php
//Destroy Session for Lockout Date to prevent by passes
unset($_SESSION['lockout_date']);
?> 

2 个答案:

答案 0 :(得分:1)

您将日期比较为字符串。您将12/30/2011之类的内容与12/11/2011之类的内容进行了比较。 PHP可以并将执行此操作,但它会将它们视为字符串。

这将产生的主要奇怪之处在于0并不像数字类型那样暗示。

此外,您的日期格式也不匹配。 MySQL会返回类似2011-12-30的内容,而你的strftime会执行类似于2011年12月30日的内容。

尝试类似

的内容
$c_date_stamp = strtotime($c_date);
$today = strtotime('today');

if($c_date_stamp <= $today) { }

这将在比较之前将日期转换为unix时间戳。另一种选择是将它们保留为字符串形式,但要对可能产生的影响感到厌倦。

例如,如果您以字符串形式执行此操作,则日期部分的大小将需要按降序排列:

if($c_date <= date('Y-m-d'))

另请注意,如果一天使用前导零并且< 10,另一个也需要这样做。

答案 1 :(得分:1)

有几件事......

  1. 发布的代码大量开放给SQL注入 攻击。在将用户数据包含在内之前,您应始终清理用户数据 数据库查询。我在下面的代码中添加了mysql_escape_string()调用 为了防止这种情况以及提及一个简单的整数转换。有 其他方法来实现这一目标。你可以通过搜索SO来学习如何 话题。
  2. 比较日期的一种简单方法是使用PHP的DateTime类。 下面的代码创建了DateTime ...的实例 当前日期和从锁定日期检索到的日期 数据库。拥有这些对象后,您可以比较这两个对象。

  3. <?php
    $id = $_GET['id'];
    // Define MySQL Information.
    
    $mysqlusername=""; // Username of MySQL database. 
    $mysqlpassword=""; // Password of the above MySQL username.
    $mysqldatabase=""; // Name of database where the table resides.
    // Connect to MySQL.
    mysql_connect("$mysqlhost", "$mysqlusername", "$mysqlpassword")or die("Could not connect to     MySQL.");
    mysql_select_db("$mysqldatabase")or die("Could not connect to selected MySQL database.");
    
    // IMPORTANT: PREVENT SQL INJECTION
    $id = mysql_escape_string($id);
    // Or, if $id is supposed to be an integer just do this ...
    // $id = (int) $id;
    
    $infosql = "SELECT * FROM premiersounds_users WHERE customer_id = $id";
    $inforesult = mysql_query($infosql) or die(mysql_error());
    $info = mysql_fetch_array($inforesult);
    
    //Get current date from server
    $c_date = new DateTime();
    $l_date = new DateTime($info['lockout_date']);
    
    //Check is Current date = lockout date
    if ($c_date->format('Y-m-d') <= $l_date->format('Y-m-d')) {
      header("location:/planner_scripts/documnet_editors/edit_weddingplanner.php?id=$id");
    } else {
      echo 'Whoops! Were sorry your account has been locked to edits because your event is less than 48 hours from now or your event has passed. To make changes to your event please contact your DJ.';
      echo'<br/>';
      echo ' Todays Date: ';
      echo $c_date;
      echo ',';
      echo ' Last Date for edits: ';
      echo $l_date;
    }
    
    ?>