PHP创建时间戳


PHP Create timestamp

我是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";       
}

我觉得你应该这么做

$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.