根据数据库中的日期检查当前日期


Check current date against date in database

我用这段代码做的是检查数据库中结束编辑的日期(例如今天的日期是12/30/11最后编辑日期是12/12/10 = LOCKED或今天的日期是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']);
?> 

有几件事…

  1. 发布的代码对sql注入是开放的攻击。在包含用户数据之前,您应该始终对其进行消毒数据库查询。我在下面的代码中添加了一个mysql_escape_string()调用为了防止这种情况,也提到了一个简单的整数强制转换。有还有其他方法。您可以通过搜索SO on了解如何操作这个话题。
  2. 比较日期的一个简单方法是使用PHP的DateTime类。下面的代码创建了DateTime的实例…一个是对象检索的锁定日期中的一个和当前日期中的一个数据库。一旦你有了这些对象,你可以比较两者。

<?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;
}
?>

您将日期作为字符串进行比较。你把12/30/2011和12/11/2011之类的东西做比较。PHP可以也会这样做,但是它会把它们当作字符串来处理。

这样做的主要奇怪之处在于不像数字类型那样隐含0。

同样,你的日期格式也不匹配。MySQL返回的是2011-12-30,而你的strftime返回的是30/12/2011。

试试

$c_date_stamp = strtotime($c_date);
$today = strtotime('today');
if($c_date_stamp <= $today) { }

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

例如,如果使用字符串形式,则日期部分的大小需要按降序排列:

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

还要注意,如果在days中使用前导零<10、另一个也要这样做