Home > Mobile >  Php check 2 time bettwen 2 time
Php check 2 time bettwen 2 time

Time:05-31

I wrote a few lines of code that doesn't work as it needs to. I need him to check if it is possible to add hours beyond those already taken. that is, earlier and later if it is possible. I will be grateful for your help.

//rocord from db
$db_start = "07:00";
$db_end = "15:00";

//new entery
$start ="06:00";
$end = "16:00"; //shuld be erturn error
//exaples data to check
//$start ="06:00";
//$end = "07:00"; shuld be return ok
//$start ="06:00";
//$end = "17:00"; shuld be return error
//$start ="15:00";
//$end = "17:00"; shuld be return ok
//$start ="13:00";
//$end = "17:00"; shuld be return error
//$start ="11:00";
//$end = "14:00"; shuld be return error


$now = date("Y-m-d");

$date_db_start = strtotime($now.' '.$db_start);
$date_db_end = strtotime($now.' '.$db_end);
$date_start = strtotime($now.' '.$start);
$date_end = strtotime($now.' '.$end);
echo $db_start.' - '.$db_end;
echo '<br>';
echo $start.' - '.$end;
echo '<br>';

if ($date_start > $date_db_start && $date_start < $date_db_end) {
  echo 'error';
}elseif($date_end < $date_db_end && $date_end > $date_db_start) {
  echo 'error';
}elseif($date_start == $date_db_end){
  echo 'do someting';
}elseif($date_end == $date_db_start){
  echo 'do something';
}else{
  echo 'do someting';
}

CodePudding user response:

    <?php
//rocord from db
$db_start = "07:00";
$db_end = "15:00";

//new entery
//$start ="06:00";
//$end = "16:00"; //shuld be erturn error
//exaples data to check
$start ="06:00";
$end = "07:00"; //shuld be return ok
//$start ="06:00";
//$end = "17:00"; shuld be return error
//$start ="15:00";
//$end = "17:00"; //shuld be return ok
//$start ="13:00";
//$end = "17:00"; shuld be return error
//$start ="11:00";
//$end = "14:00"; //shuld be return error


$now = date("Y-m-d");

$date_db_start = strtotime($now.' '.$db_start);
$date_db_end = strtotime($now.' '.$db_end);
$date_start = strtotime($now.' '.$start);
$date_end = strtotime($now.' '.$end);
echo $db_start.' - '.$db_end;
echo '<br>';
echo $start.' - '.$end;
echo '<br>';

if ($date_start > $date_db_start && $date_start < $date_db_end) {
  echo 'error';
} elseif($date_db_start>$date_start and $date_db_end<$date_end) {
    echo 'error';
}elseif($date_end < $date_db_end && $date_start > $date_db_start) {
  echo 'error';
}elseif($date_start == $date_db_end){
  echo 'do someting';
}elseif($date_end == $date_db_start){
  echo 'do something';
}else{
  echo 'do someting';
}
  • Related