在非对象错误时调用成员函数getTimestamp()

时间:2019-02-14 11:42:11

标签: php date datetime timestamp

我正在尝试执行计算,以使用日期格式正确的if语句检查日期是否在其他两个日期之间,但是我似乎仍然遇到时间戳错误。对于如何解决它的任何帮助将不胜感激。

$Query = mysqli_query($con, "SELECT * FROM tblrental WHERE `CARID` = '".$Car."'");
$row3 = mysqli_fetch_assoc($Query);
$ARTakeOut = $row3\['TakeOutDate'\];
$ARReturn = $row3\['ReturnDate'\];
$ARDaysRented = $row3\['DaysRented'\];
$ARDaysRented = (round($ARDaysRented/2));
$MidDate = date('Y-m-d', strtotime($ARTakeOut . ' + '.$ARDaysRented.' days'));

echo $MidDate; ?> <br> <?PHP
echo $ARTakeOut; ?> <br> <?PHP
echo $ARReturn; ?> <br> <?PHP




if (
  $MidDate->getTimestamp() > $ARTakeOut->getTimestamp() && 
  $MidDate->getTimestamp() < $ARReturn->getTimestamp()){
  echo "is between";
}else{
   echo "NO GO!";  
}

enter image description here

1 个答案:

答案 0 :(得分:4)

$MidDate$ARTakeOut$ARReturn都是字符串($MidDatedate的输出,其他是从数据库中读取的),而不是{ {1}}个对象。如果要使用DateTime,则需要将它们变成DateTime对象:

getTimeStamp

Demo on 3v4l.org

请注意,在您的$ARTakeOut = new DateTime($row3['TakeOutDate']); $ARReturn = new DateTime($row3['ReturnDate']); $ARDaysRented = $row3['DaysRented']; $ARDaysRented = (round($ARDaysRented/2)); $MidDate = clone $ARTakeOut; $MidDate->modify("+ $ARDaysRented days"); echo $MidDate->format('Y-m-d'); ?> <br> <?PHP echo $ARTakeOut->format('Y-m-d'); ?> <br> <?PHP echo $ARReturn->format('Y-m-d'); ?> <br> <?PHP \之前,我已经删除了多余的[