dty5753 2019-05-21 14:52
浏览 48
已采纳

如何检查两个日期之间是否已经出租

I'm trying to compare two dates on for the start of the rent and the end of the rent but with a database where some vehicles could have already rented at thoses dates

i've already have a code that block the user to book a car between two dates but after that it's still blocked

if( empty($content) ){
        extract($_POST); // here i extract my _post
        if( !empty($idmembre) ){

            $vehicule = execReq( "SELECT * FROM vehicule WHERE idvehicule=:idvehicule", array(
                'idvehicule' => $idvehicule
            ));
            $infoVehicule = $vehicule->fetch();
            $agence = $infoVehicule['agences_idagences'];
            $timestamp1 = strtotime($date_heure_depart);
            $timestamp2 = strtotime($date_heure_fin); // here i create two timestamps to compare with what i have en my database
            $date_deja_prise = execReq( "SELECT * FROM commande WHERE vehicule_idvehicule=:idvehicule", array(
                'idvehicule' => $idvehicule
            ));
            while( $date = $date_deja_prise->fetch() ){
                if( !empty($date) ){ // here if one vehicle has a date of rent it checks evry one to check if the date is taken
                    $date_debut = strtotime($date['date_heure_depart']);
                    $date_fin = strtotime($date['date_heure_fin']);
                    if( ($date_debut <= $timestamp1 || $timestamp2 <= $date_fin) ){
                        $content .= '<div class="alert alert-danger">Le véhicule '.$infoVehicule['titre'].' est déjà louer du '.$date['date_heure_depart'].' au '.$date['date_heure_fin'].' inclu</div>';
                    }
                }
            }
            if( empty($content) ){
                $nb_de_jour_timestamp = $timestamp2 - $timestamp1;
                $nb_de_jour = $nb_de_jour_timestamp/86400;
                $prix_journalier = $nb_de_jour * $infoVehicule['prix_journalier'];
                execReq( "INSERT INTO commande VALUES(NULL, $idmembre, $idvehicule, $agence, :date_heure_depart, :date_heure_fin, $prix_journalier, now())", array(
                    'date_heure_depart' => date("Y-m-d", $timestamp1),
                    'date_heure_fin' => date("Y-m-d", $timestamp2)
                ));
                $content = '<div class="alert alert-success">Votre reservation a été effectué !</div>';
            }
        } else {
            $content .= '<div class="alert alert-danger">Pour passer une commande vous devez créer un compte <a href="" data-toggle="modal" data-target="#inscription">cliquez ici</a> pour vous en créer un</div>';
        }
    }

so basicaly it can create a line in my phpmyadmin page where the car is booked for some period of time and after that if i try to book an other car but all the dates are differents i can't

  • 写回答

1条回答 默认 最新

  • douhuang4166 2019-05-21 15:01
    关注

    I think your problem is this line if( ($date_debut <= $timestamp1 || $timestamp2 <= $date_fin) ){. You are only checking if thenew start time is <= than an existing start time and if the new end time is >= than an existing one. You need to check if either the start or end date is between an existing date.

    if( ($date_debut <= $timestamp2 && $timestamp1 <= $date_fin) ){

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错