+ -
当前位置:首页 → 问答吧 → 两个时间的比较 请高手赐教

两个时间的比较 请高手赐教

时间:2011-11-17

来源:互联网

我用的是Mysql数据库现在数据库里面有两个字段 分别是formTime(开始时间)和toTime(结束时间)
PHP中的参数用的是time1(开始时间) 和time2(结束时间)
现在想判断插入的时间不在数据库的开始时间和结束时间范围内
如果时间正确则插入时间 如果错误将弹出对话框说时间错误(如果有更好的也可以)
我能想到的代码如下
PHP code

$sql1 ="SELECT fromTime,toTime  FROM operation WHERE date='$date' and username='$_POST[name]'";
    mysql_query($sql1);
    $result=mysql_query($sql);
    while($row=mysql_fetch_assoc($result)){
    
    $fromTime=$row[fromTime];
    $toTime=$row[toTime];
    if(strtotime($time1)>strtotime($fromTime)&&strtotime($time2)<strtotime($toTime) ){

    }elseif(strtotime($time1)>strtotime($formTime)&&strtotime($time2)>strtotime($toTime1)){

    }elseif(strtotime($time1)<strtotime($formTime1)&&strtotime($time2)<strtotime($toTime1)){

    }elseif(strtotime($time1)<strtotime($formTime1)&&strtotime($time2)>strtotime($toTime1)){

    }else{


    
    $sql="insert into operation(username,date,fromTime ,toTime,content,progress) values('$_POST[name]','$date','$time1','$time2','$_POST[content]','$_POST[progress]')";
    mysql_query($sql);
    }

作者: liuwanxu   发布时间: 2011-11-17

PHP code

$sql ="SELECT count(*) exists  FROM operation WHERE ((fromTime < '{$time1}' and toTime>'{$time1}') or (fromTime < '{$time2}' and toTime>'{$time2}')) and date='$date' and username='$_POST[name]'";
    mysql_query($sql1);
    $result=mysql_query($sql);
    if($row=mysql_fetch_assoc($result) && $row['exists']){
        //存在时间交叉的数据
    }

作者: moocky   发布时间: 2011-11-17

相关阅读 更多