Need help to change booking system php

So I have the php code which works well BUT:
Users now want to make a booking IF a current user has left the location.
The booking system is for boats using facilities and predicated on a booking being unique based on the date and location being unique.
So now if the new required date clashes with the desired date and location BUT the existing booking Leave Time is past compared to the current system time then the booking system should allow the new booking?

Current PHP below so I would welcome any guidance on how I could make the above happen:
add_filter(‘frm_validate_field_entry’, ‘two_fields_unique’, 999999, 2);
function two_fields_unique( $errors, $posted_field ) {
$first_field_id = 539;
$second_field_id = 663;
$leave_grid_id = 553;

if ( $posted_field->id == $first_field_id ) {
$_POST[‘item_meta’][$second_field_id] = $_POST[‘item_meta’][540].$_POST[‘item_meta’][638];
$booking_date = date(“Y-m-d”,strtotime($_POST[‘item_meta’][$second_field_id]));

$entry_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0;
 global $wpdb;

 
  $values_used=$wpdb->get_var("SELECT m1.item_id FROM wp_frm_item_metas as m1 left join wp_frm_item_metas as m2 on(m2.item_id =m1.item_id and m1.field_id = $first_field_id and m2.field_id = $second_field_id ) left join wp_frm_item_metas as m3 on(m3.item_id =m1.item_id and m1.field_id = $first_field_id and m3.field_id = $leave_grid_id ) WHERE m1.item_id !=".$entry_id." AND ( ( m1.field_id=$first_field_id AND m1.meta_value='".$_POST['item_meta'][ $first_field_id ]."' ) and (( m2.field_id=$second_field_id AND date(m2.meta_value)='".$booking_date."' ) or  ( m3.field_id=$leave_grid_id AND date(m3.meta_value)='".$booking_date."' ) ))");
 
if ( ! empty( $values_used ) ) {
	$errors[ 'field'. $first_field_id ] = 'This grid location and date are already booked,choose another grid and date';
	$errors[ 'field'. $second_field_id ] = 'This grid location and date are already booked,choose another grid and date';
}

}
return $errors;
}

Sponsor our Newsletter | Privacy Policy | Terms of Service