0

I´m trying to create script to compare any ours from json and function difference hours to extract if actual time it´s minor that hour in json or higher that time json. to do this i´m buildg a function with DateTime() php and pass to this function two arguments:

$diferencia = $this->diferenciaFechas($hora_actual, $value["hora_inicio"]);

function diferenciaFechas($horaInicio, $horaFin) {
    $hora_inicio = new Datetime($horaInicio);
    $hora_fin = new Datetime($horaFin);

    $tiempo = $hora_inicio->diff($hora_fin);

    return $tiempo;
}

and i´m checking my results with:

if($provincia == $value["localidad"] && $diferencia->h < 0 || $provincia == $value["localidad"] && $diferencia->h > 0){
    $fuera_horario = true;
    $rama = "fuera horario";
}else{
    echo $value["localidad"];
    echo $value["hora_inicio"] . "<br>";
}

always my script go to else block:

this: $value["localidad"] its value from my foreach:

foreach ($festivos as $value) {

    if($viernes == "Friday"){
        if($hora_actual < $value["hora_inicio_viernes"] || $hora_actual >= $value["hora_fin_viernes"]){
            $fuera_horario = true;
        }
    }else{
        $diferencia = $this->diferenciaFechas($hora_actual, $value["hora_inicio"]);

        //echo $diferencia->h;


                

        if($provincia == $value["localidad"] && $diferencia->h < 0 || $provincia == $value["localidad"] && $diferencia->h > 0){
            $fuera_horario = true;
            $rama = "fuera horario";
        }else{
            echo $value["localidad"];
            echo $value["hora_inicio"] . "<br>";
        }
    }
}

and $festivos it´s json_decode() from file_get_contents('include/festivos.json');

any data from to this json its:

{
    "cp": "17",
    "localidad": "Gerona",
    "fecha": "2022-12-26",
    "hora_inicio": "09:00:00",
    "hora_fin": "14:00:00",
    "hora_inicio_viernes": "09:00:00",
    "hora_fin_viernes": "14:00:00"
},
{
    "cp": "18",
    "localidad": "Granada",
    "fecha": "2022-12-15",
    "hora_inicio": "12:01:00",
    "hora_fin": "12:00:00",
    "hora_inicio_viernes": "09:00:00",
    "hora_fin_viernes": "14:00:00"
},
{
    "cp": "18",
    "localidad": "Granada",
    "fecha": "2023-01-01",
    "hora_inicio": "09:00:00",
    "hora_fin": "14:00:00",
    "hora_inicio_viernes": "09:00:00",
    "hora_fin_viernes": "14:00:00"
},
{
    "cp": "19",
    "localidad": "Guadalajara",
    "fecha": "2022-12-26",
    "hora_inicio": "09:00:00",
    "hora_fin": "14:00:00",
    "hora_inicio_viernes": "09:00:00",
    "hora_fin_viernes": "14:00:00"
},

i need get if $hora_inicio from my json it´s minor or bigger than actual time to know if i´m in labor time or not.

I´m sorry for my bad english.

Thanks for help me

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
scorpions78
  • 553
  • 4
  • 17
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Dec 15 '22 at 12:54
  • If you are using AND and OR in an IF statement, use brackets `()` to emphasise what is being ANDed and what is being OR'd – RiggsFolly Dec 15 '22 at 12:56
  • @RiggsFolly in my code editor my identation it´s ok... – scorpions78 Dec 15 '22 at 12:56
  • So maybe `if( ($provincia == $value["localidad"] && $diferencia->h < 0 ) || ($provincia == $value["localidad"] && $diferencia->h > 0) ){` – RiggsFolly Dec 15 '22 at 12:57
  • @RiggsFolly i think that your code it´s ok and solve my problem, but i need check it more. Thanks for help me – scorpions78 Dec 15 '22 at 13:01

0 Answers0