Condition in php with function and strtotime does not validate a period of dates

0

I have a date that I want to condition. 12-27-2016. I want to check if within the period of dates from 20-12-2015 to 12-20-2016 it is fulfilled or not, as well as from 20-12-2016 to 20-12-2017. In the case of the first period 2015-2016, the date is not fulfilled. But if you must meet the 2016-2017 period and however you indicate that it is out of period.

Here is the screen and the code.

I also attach the code in PHP

<?php

$hoy               = date('d-m-Y');
$fi                = date("20-12-2001");
$fecha_ingreso     = date("d-m-Y", strtotime($fi));
$anio_actual       = date("Y");
$fecha_aniversario = date("d-m-" . $anio_actual . "", strtotime($fi));

echo "Fecha de Ingreso: " . $fecha_ingreso . "</br>";
echo "Fecha de Aniversario en el año 2016: " . $fecha_aniversario . "</br>";
echo "Comparar si el dia de hoy " . $hoy . " el empleado cumple Aniversario:  </br>";

if ($fecha_aniversario == $hoy) {
    echo "Hoy " . $hoy . " es su aniversario </br>";
} else {
    echo "Hoy " . $hoy . " no es su aniversario </br>";
}

$periodo2015 = strtotime('-1 year', strtotime($fecha_aniversario));
$periodo2015 = date('d-m-Y', $periodo2015);

$periodo2017 = strtotime('+1 year', strtotime($fecha_aniversario));
$periodo2017 = date('d-m-Y', $periodo2017);



echo " Comparar si " . $hoy . "  cumple el rango de :" . $periodo2015 . " al periodo " . $fecha_aniversario . " </br> ";


$fecha1 = new DateTime($hoy);
$fecha1 = $fecha1->format("d-m-Y");

$f2015 = new DateTime($periodo2015);
$f2015 = $f2015->format("d-m-Y"); //=> 2015

$f2016 = new DateTime($fecha_aniversario);
$f2016 = $f2016->format("d-m-Y"); //=> 2016

$f2017 = new DateTime($periodo2017);
$f2017 = $f2017->format("d-m-Y"); //=2017


function comprobarPeriodo20152016($fecha1, $f2015, $f2016)
{

    return $fecha1 >= $f2015 && $fecha1 <= $f2016;
}


if (comprobarPeriodo20152016($fecha1, $f2015, $f2016)) {

    echo " " . $fecha1 . " Esta dentro del periodo " . $f2015 . "-" . $f2016 . "</br>";

} else {

    echo " " . $fecha1 . " Esta fuera del periodo " . $f2015 . "-" . $f2016 . "</br>"; // <= Resultado
}

echo "</br>";

echo " Comparar si " . $hoy . "  cumple el rango de :" . $fecha_aniversario . " al periodo " . $periodo2017 . " </br> ";

function comprobarPeriodo20162017($fecha1, $f2016, $f2017)
{

    return $fecha1 >= $f2016 && $fecha1 <= $f2017;
}


if (comprobarPeriodo20162017($fecha1, $f2016, $f2017)) {

    echo " " . $fecha1 . " Esta dentro del periodo " . $f2016 . "-" . $f2017 . "</br>";

} else {

    echo " " . $fecha1 . " Esta fuera del periodo " . $f2016 . "-" . $f2017 . "</br>"; // <= Resultado
}

echo "</br>";

?>

The first condition is fulfilled. It is not within the period 2015-2016 The second should be fulfilled. It is within the 2016-2017 period and it does not Thanks for your comments you are welcome

    
asked by Juan Pablo Bustamante Luna 27.12.2016 в 23:36
source

1 answer

1

I see that you use two functions to do the same. The idea of the functions is that if the algorithm or the procedure is repeated and only the values are changed, it becomes a SINGLE function and that the different values are passed in the parameters. Unify and change a bit the function that makes period checking, converting to timestamp the three dates and then the comparison is made.

I leave the full script.

<?php

$hoy               = date('d-m-Y');
$fi                = date("20-12-2001");
$fecha_ingreso     = date("d-m-Y", strtotime($fi));
$anio_actual       = date("Y");
$fecha_aniversario = date("d-m-" . $anio_actual . "", strtotime($fi));

echo "Fecha de Ingreso: " . $fecha_ingreso . "</br>";
echo "Fecha de Aniversario en el año 2016: " . $fecha_aniversario . "</br>";
echo "Comparar si el dia de hoy " . $hoy . " el empleado cumple Aniversario:  </br>";

if ($fecha_aniversario == $hoy) {
    echo "Hoy " . $hoy . " es su aniversario </br>";
} else {
    echo "Hoy " . $hoy . " no es su aniversario </br>";
}

$periodo2015 = strtotime('-1 year', strtotime($fecha_aniversario));
$periodo2015 = date('d-m-Y', $periodo2015);

$periodo2017 = strtotime('+1 year', strtotime($fecha_aniversario));
$periodo2017 = date('d-m-Y', $periodo2017);



echo " Comparar si " . $hoy . "  cumple el rango de :" . $periodo2015 . " al periodo " . $fecha_aniversario . " </br> ";


$fecha1 = new DateTime($hoy);
$fecha1 = $fecha1->format("d-m-Y");

$f2015 = new DateTime($periodo2015);
$f2015 = $f2015->format("d-m-Y"); //=> 2015

$f2016 = new DateTime($fecha_aniversario);
$f2016 = $f2016->format("d-m-Y"); //=> 2016

$f2017 = new DateTime($periodo2017);
$f2017 = $f2017->format("d-m-Y"); //=2017


function comprobarPeriodos($fecha_actual, $fecha_inicio, $fecha_fin){
        $inicio_ts = strtotime($fecha_inicio);
        $fin_ts = strtotime($fecha_fin);
        $fecha_actual_ts = strtotime($fecha_actual);
        return (($fecha_actual_ts >= $inicio_ts) && ($fecha_actual_ts <= $fin_ts));
    }


if (comprobarPeriodos($fecha1, $f2015, $f2016)) {

    echo " " . $fecha1 . " Esta dentro del periodo " . $f2015 . "-" . $f2016 . "</br>";

} else {

    echo " " . $fecha1 . " Esta fuera del periodo " . $f2015 . "-" . $f2016 . "</br>"; // <= Resultado
}

echo "</br>";

echo " Comparar si " . $hoy . "  cumple el rango de :" . $fecha_aniversario . " al periodo " . $periodo2017 . " </br> ";


if (comprobarPeriodos($fecha1, $f2016, $f2017)) {

    echo " " . $fecha1 . " Esta dentro del periodo " . $f2016 . "-" . $f2017 . "</br>";

} else {

    echo " " . $fecha1 . " Esta fuera del periodo " . $f2016 . "-" . $f2017 . "</br>"; // <= Resultado
}

echo "</br>";

?>

    
answered by 28.12.2016 / 01:39
source