如何从数据库和DateTime中区分记录

时间:2018-05-31 01:26:42

标签: php datetime

我尝试在数据库和DateTime

的记录之间进行差异

enter image description here 这是我的代码

<?php
$link = mysqli_connect("localhost", "root", "", "abb");
$result = mysqli_query($link, "SELECT * FROM submission_abb");
$row=mysqli_fetch_array($result);
$submission = $row['submission'];
$selesai = new DateTime();
$hitung = $submission->diff($selesai);
$result = $hitung->format('Y-m-d');
if($hitung->d <=3){
echo $result . "Ontime";
}else{
echo $result . "Over time";
}
?>

1 个答案:

答案 0 :(得分:0)

您的问题是您在字符串值上调用diff(),因为$submission包含一个字符串的日期值。您只能在diff()对象上拨打DateTime()

<?php
$link = mysqli_connect("localhost", "root", "", "abb");
$result = mysqli_query($link, "SELECT * FROM submission_abb");
$row=mysqli_fetch_array($result);
$submission = new DateTime($row['submission']);
$selesai = new DateTime();
$hitung = $submission->diff($selesai);
$result = $hitung->format('Y-m-d');
if($hitung->d <=3){
echo $result . "Ontime";
}else{
echo $result . "Over time";
}
?>