0

我希望能够在插入另一条记录之前检查表中最新记录的日期时间字段。当插入记录时,我使用它来填充日期时间字段:

$date = date("Y-m-d H:i:s");

至于现在,我对其他字段进行了验证,但我也想在datetime字段上添加验证。它应该检查我表上的最新记录并检查datetime字段和 $date = date("Ymd H:i:s");的差异。如果差异是 5 秒或更短,它现在应该允许向表中插入新记录。将显示如下错误消息:

    }else if(trim($checktime) == '') {
    $error = 'Please wait 5 seconds before insert'; 
   } if($error == '') { //Run the code

对于上面的示例,$checktime 会是什么样子?

4

2 回答 2

2

首先查询数据库的最新日期时间。像这样的东西应该可以解决问题:SQL:

SELECT MAX(`mydate`) AS mydate FROM `tablename`;
or
SELECT `mydate` FROM `tablename` ORDER BY `mydate` DESC LIMIT 1;
or if you have a primary key auto-increment column called id this may be fastest:
SELECT `mydate` FROM `tablename` ORDER BY `id` DESC LIMIT 1;

<?php
$last_insert_time = strtotime(); # Use the result from the database query as the parameter
$now = time();

# If the difference between the last inserted entry and the current time is less
# than five seconds perform an error action.
if (($now - $last_insert_time) < 5) {
    $error = 'Please wait 5 seconds before insert';
}
于 2013-01-10T11:15:34.617 回答
0

你可以这样使用

1)首先使用此查询获取最后记录日期值

SELECT dateVal FROM tablename ORDER BY id DESC LIMIT 0, 1;

2)通过使用这个找到两个日期的差异

$timeFirst  = strtotime($dateVal);
$timeSecond = strtotime(date('Y-m-d H:i:s'));
$checktime= $timeSecond - $timeFirst;
于 2013-01-10T11:10:10.253 回答