Php 检查数据是否比当前时间早
我试图检查数据库中的某一行和某一列的时间是否比今天早,然后回显成功,但它回显成功的日期是明天 代码Php 检查数据是否比当前时间早,php,arrays,date,time,strtotime,Php,Arrays,Date,Time,Strtotime,我试图检查数据库中的某一行和某一列的时间是否比今天早,然后回显成功,但它回显成功的日期是明天 代码 // Set variable for the time $timenow = date('l jS \of F Y h:i:s A'); // Start Expired password check and drop function function cronexec_expired ($timenow) { include('../../config.p
// Set variable for the time
$timenow = date('l jS \of F Y h:i:s A');
// Start Expired password check and drop function
function cronexec_expired ($timenow) {
include('../../config.php');
// Open up a new MySQLi connection to the MySQL database
mysql_connect($dbHost, $dbUsername, $dbPassword);
mysql_select_db($dbTable);
// Query to check the status of the code input
$expiry_check = "SELECT * FROM code_log WHERE status='Unused'";
// Run the query
$expiry_checkexec = mysql_query($expiry_check);
while($expiry_possibles = mysql_fetch_array($expiry_checkexec)) {
echo $expiry_possibles[6] . '<br /><br />';
echo $timenow . '<br /><br />';
if (date('l jS \of F Y h:i:s A', strtotime($expiry_possibles[6]) < $timenow)) {
echo "Success";
}
}
}
Monday 9th of September 2013 12:46:20 PM
现在是时候了
Sunday 8th of September 2013 01:35:22 PM
任何建议都将不胜感激我不明白你为什么要使用完整的日期,没有必要。相反,使用unix时间戳怎么样?看见比较可能很难比较完整的日期字符串 我总是发现使用
time()
要容易得多,因为它基本上是比较两个数字
$current_time = time();
$expiry_possibles_timestamp = strtotime($expiry_possibles[6]);
if ($current_time > $expiry_possibles_timestamp)
{
// Older than current time
}
我已经修改了下面的代码,请尝试一下。请记住,您将需要PHP5.3及以上版本
function cronexec_expired($timenow)
{
include('../../config.php');
// Open up a new MySQLi connection to the MySQL database
mysql_connect($dbHost, $dbUsername, $dbPassword);
mysql_select_db($dbTable);
// Query to check the status of the code input
$expiry_check = "SELECT * FROM code_log WHERE status='Unused'";
// Run the query
$expiry_checkexec = mysql_query($expiry_check);
while ($expiry_possibles = mysql_fetch_array($expiry_checkexec))
{
echo $expiry_possibles[6] . '<br /><br />';
echo $timenow . '<br /><br />';
$datetime = DateTime::createFromFormat('l jS \of F Y h:i:s', $expiry_possibles[6]);
if (!$datetime) continue; // Skip this execution as we cannot parse the date, so we will not trust it.
if (time() > $datetime->getTimestamp())
{
echo "Database row is in the past";
} else
{
echo "Database row is in the future";
}
}
}
函数cronexec_已过期($timenow)
{
包括('../config.php');
//打开到MySQL数据库的新MySQLi连接
mysql_connect($dbHost、$dbUsername、$dbPassword);
mysql\u select\u db($dbTable);
//查询以检查代码输入的状态
$expiry\u check=“从代码日志中选择*,其中status='Unused'”;
//运行查询
$expiry\u checkexec=mysql\u查询($expiry\u check);
而($expiry\u possibles=mysql\u fetch\u数组($expiry\u checkexec))
{
echo$expiration_possibles[6]。
;
echo$timenow.“
”;
$datetime=datetime::createFromFormat('l jS\of F Y h:i:s',$expiry_possibles[6]);
如果(!$datetime)继续;//跳过此执行,因为我们无法分析日期,因此我们将不信任它。
如果(时间()>$datetime->getTimestamp())
{
echo“数据库行在过去”;
}否则
{
echo“数据库行在将来”;
}
}
}
虽然我还没有测试过,但这应该是可行的。日期是如何存储在数据库中的?它应该是MySQL格式(YYYY-MM-DD HH:MM:SS)或unix时间戳格式。我建议使用
DateTime
和DateInterval
类
如果差异正好是一天,您可以对其进行比较:
$expiry = new DateTime($expiry_possibles[6]);
if ($expiry->diff($time_now)->format('%d') == "1") ...
反之亦然。还有一个程序接口,其形式为date\u diff()
更多信息:
我使用的是全长日期,因为单元格数据在用户可以看到的日志中使用。如果我可以使用全长日期或将全长日期转换为unix时间戳,我更喜欢这样做。不过,最好对您的代码使用更易于阅读的比较。您有什么建议吗?是的,请使用我提供的代码,如果
strotime()
能够正确解析数据库中的日期,它应该可以工作。这有帮助吗,如果有,请接受它作为正确答案。我看到了这一点并尝试了它,但没有效果,使用您的代码,我得到的错误如下PHP致命错误:调用成员函数diff()在第26行的D:\Websites\Sites\reveal\resources\sec\cron.php中的非对象上
是的,我提供的只是部分代码,您需要在任何地方使用DateTime对象。例如,您可以这样做:$expiry=newdatetime($expiry_possibles[6]);那就用这个。我将在解释中添加这一点,但建议阅读更多关于DateTime类的内容(答案中已经有链接)。