我是php编程的初学者。我正在尝试比较保存在数据库中的2个时间戳类型, 这是我的代码:
$sdate = "{$_POST['sdatey']}-{$_POST['sdatem']}-{$_POST['sdated']} $stime";
$edate = "{$_POST['edatey']}-{$_POST['edatem']}-{$_POST['edated']} $etime";
$startdate = strtotime($sdate);
$enddate = strtotime($edate);
$diff = floor($enddate - $startdate);
if($diff > 0){
$sql = "INSERT INTO `leave` (TypeID, StatusID, StaffID, StartDate, EndDate, CreateDate) VALUES ('1','1', {$_SESSION['user']['ID']} , '$sdate', '$edate', '$date')";
header('Location:ApplyLeave-Step3.php');
}
else {
echo "wrong";
}
答案 0 :(得分:1)
我认为你应该这样做
$startdate = date('Y-m-d h:i:s',strtotime($sdate));
$enddate = date('Y-m-d h:i:s',strtotime($edate));
//I have changed the startdate and enddate because you want to insert it into DB
$diff = abs(strtotime($enddate ) - strtotime($startdate));
$sql = "INSERT INTO `leave`
(TypeID, StatusID, StaffID, StartDate, EndDate, CreateDate)
VALUES
('1','1','".$_SESSION['user']['ID']."', '$startdate', '$enddate', '$date')";
//$date: I cannot see it is anywhere initialized.