将CURRENT_TIMESTAMP与integer进行比较


compare CURRENT_TIMESTAMP with integer

嗨,我想知道如何将CURRENT_TIMESTAMP值与int进行比较,我在表book中有一个int值,比方说它被称为b_time,在表subscriber中我有一个CURRENT_IMESTAMP值,比如说它被称作s_time,b_time是允许借书的时间,比方说5意味着5天,s_time是订阅者借书的时候,角色是,如果离书的末尾还有2天的允许借用时间(b_time),就会发生一些事情,所以我想将s_time与b_time进行比较,这是我的想法,但我不知道如何实现它,即使在数据库表声明中也是如此:

(php)

$t = b_time-2 ;
if (CURRENT_TIMESTAMP() == s_time+$t) {
    //do somthing
}

我知道这不会运行,但我有一个代码可以做同样的事情。

谢谢。

首先获取书籍的借用时间,即s_time,并将其存储为$s_time

现在提取允许借阅的书籍,即b_time,并将其存储为$b_time

现在比较一下当前借阅的书是否有不到两天的时间归还图书馆

所以比较逻辑将是

if(time() >=  $s_time + (($b_time - 2) * 86400) ) ) {
   //this means borrowed book has two days to return now

}

使用time()函数http://php.net/manual/en/function.time.php或使用DateTime类http://php.net/manual/en/class.datetime.php,例如:

$dateTime  = new DateTime('now');
$timeStamp = $dateTime->format('U');

"U"-Unix大纪元后的秒数(1970年1月1日00:00:00 GMT)

更新#1:检查是否还有2天的时间可以结束:

$dateTime         = new DateTime('now');
$dateTimeBookLeft = new DateTime($b_time);
$interval  = $dateTime->diff($dateTimeBookLeft);
$countDays = $interval->d;
if ($counDays == 2) {
   ...
}

更新#2

$currentDate = new DateTime('now'); //current date
$b_issued    = new DateTime('2015-12-08 10:00:00');  //date when the book was issued
$b_time      = 5; //number of days for which has been given a book
$interval    = $b_issued->diff($currentDate);
$b_left_days =  $b_time - $interval->d;
if ($b_left_days == 2) {
  //...
}