Php 将时间添加到datetime并签入if语句

Php 将时间添加到datetime并签入if语句,php,datetime,Php,Datetime,我被卡住了。我有一个数据库,在comments表中有columndatetime。基本上,当用户添加一条新注释时,它会插入到此表中,并存储日期时间。所以我现在要做的是检查,假设他上次发表评论已经过去了1分钟。但我在if声明中一直在说实话。 好。在发布之前,这是最后一次检查。输出不符合预期。 我的密码 $limits=新限制(); $user=新用户(); $time=新的日期时间(); $time->add(新的日期间隔('PT'.1.M'); $stamp=$time->format('Y-m

我被卡住了。我有一个数据库,在
comments
表中有column
datetime
。基本上,当用户添加一条新注释时,它会插入到此表中,并存储日期时间。所以我现在要做的是检查,假设他上次发表评论已经过去了1分钟。但我在if声明中一直在说实话。 好。在发布之前,这是最后一次检查。输出不符合预期。 我的密码

$limits=新限制();
$user=新用户();
$time=新的日期时间();
$time->add(新的日期间隔('PT'.1.M');
$stamp=$time->format('Y-m-dh:i:s');
$limit=$limits->check_comment_limit($user->loggedInUser());
如果($limit->time<$stamp){
回声“刹车”;
}
//用于调试
回音$邮票
'//2014-03-18 13:38:41 echo$限制->时间//2014-03-18 01:37:37

很明显,
$limit->time
$stamp
小。使用
time()
很简单,
time()+60
但是如何使用datetime来实现这一点呢?

我想这就是您想要做的:

<?php
 date_default_timezone_set('UTC'); 

 // Get current datetime.
 $now = new DateTime("now");

 // Get last comment datetime.
 $comment = new DateTime('2014-03-18 13:26:00');

 // Get interval limit between comments.
 $limit = new DateInterval('PT1M');

 // Calculate the difference between the current and last commented datetime.
 $diff = $now->diff($comment);

 // Calculate the total minutes.
 $minutes = $diff->days * 24 * 60;
 $minutes += $diff->h * 60;
 $minutes += $diff->i;

 // Debug output
 echo $now->format('Y-m-d H:i:s').'<br>';
 echo $comment->format('Y-m-d H:i:s').'<br>';
 echo $limit->i.'<br>';
 echo $minutes.' minutes <br>';

 // Limit smaller than difference? Next comment is allowed.
 if($limit->i <= $minutes) {
    echo "Your comment has been saved!";
 } else {
echo "Your aren't allowed to comment now. Wait ".intval($limit->i*60 - 
      $diff->format('%s'))." seconds until you are allowed to comment again.";
 }
 ?>

$stamp是一个字符串。是否要比较DateTime值或DateInterval值?为第一个解决方案和第二个解决方案添加了修复程序。
<?php
 date_default_timezone_set('UTC'); 

 // Get current datetime.
 $now = new DateTime("now");

 // Get last comment datetime.
 $comment = new DateTime('2014-03-18 13:26:00');

 // Get interval limit between comments.
 $limit = new DateInterval('PT1M');

 // Calculate the difference between the current and last commented datetime.
 $diff = $now->diff($comment);

 // Calculate the total minutes.
 $minutes = $diff->days * 24 * 60;
 $minutes += $diff->h * 60;
 $minutes += $diff->i;

 // Debug output
 echo $now->format('Y-m-d H:i:s').'<br>';
 echo $comment->format('Y-m-d H:i:s').'<br>';
 echo $limit->i.'<br>';
 echo $minutes.' minutes <br>';

 // Limit smaller than difference? Next comment is allowed.
 if($limit->i <= $minutes) {
    echo "Your comment has been saved!";
 } else {
echo "Your aren't allowed to comment now. Wait ".intval($limit->i*60 - 
      $diff->format('%s'))." seconds until you are allowed to comment again.";
 }
 ?>
<?php
date_default_timezone_set('UTC'); 

// Get current datetime.
$now = new DateTime("now");

// Get last comment datetime.
$comment = new DateTime('2014-03-18 16:45:00');

// Get and add interval limit to comments.
$limit = new DateInterval('PT1M');
$comment->add($limit);

// Debug output
echo $now->format('Y-m-d H:i:s').'<br>';
echo $comment->format('Y-m-d H:i:s').'<br>';

// Limit smaller than difference? Next comment is allowed.
if($comment <= $now) {
    echo "Your comment has been saved!";
} else {
    echo "Your aren't allowed to comment now. On ".$comment->format('Y-m-d H:i:s')." you 
            are allowed to comment again.";
}
?>