Doubt with dates in php

-1

I have a question about how to make a function with the dates. It is a system to ask for vacations and as the end of the year arrives, there will be people who in 2019 will want to ask only for the days left in 2018. My intention in the system is the following:

In the application form they have a select field to choose the year they want to take vacations from. My intention is that until the end of the year, this field is read only, when we have entered 2019, that field is a selection to choose between 2019 and 2018, and that in March 2019 that field will be read again.

My function is as follows:

date_default_timezone_set('Europe/Madrid');

$fecha_actual = date("Y-m-d H:i:s");

$anno_actual = date("Y");

$anno_siguiente = date("Y") + 1;

$date = new Datetime("$anno_siguiente-01-01 00:00:00");
$findeanno = date_format($date, 'Y-m-d H:i:s');

$datemarzo = new Datetime("2019-03-01 00:00:00");
$marzo = date_format($marzo, 'Y-m-d H:i:s');

if($fecha_actual < $findeanno){
  //campo readonly
}

Any help?

    
asked by Xerox 20.12.2018 в 16:00
source

3 answers

0

Solved by adding a variable, such as $ anioDeactivated to which you assign the string "disabled" depending on the date to current. Next you will see your code edited according to the variable $ anioDeactivated:

<?php
date_default_timezone_set('Europe/Madrid');

$fecha_actual = date("Y-m-d H:i:s");

$anno_actual = date("Y");

$anno_siguiente = date("Y") + 1;

$date = new Datetime("$anno_siguiente-01-01 00:00:00");
$findeanno = date_format($date, 'Y-m-d H:i:s');

$datemarzo = new Datetime("2019-03-01 00:00:00");

$anioDesactivado = ""; //aqui definimos si el select debe ser de solo lectura

if( $fecha_actual < $findeanno || $fecha_actual > $datemarzo ){
  //si se cumplen las condiciones del if el select será desactivado
  $anioDesactivado = "disabled";
}
//Al escribir el html del <option> agregamos la variable $desavtivado que tendrá el valor "disabled" o no dependiendo del condicional anterior
echo"
<select name='anno'>
  <option value='2018'>2018</option>
  <option value='2019' $anioDesactivado>2019</option>
</select>";

I hope this helps you.

    
answered by 20.12.2018 в 17:44
0

I concentrated on the following:

if($fecha_actual < $findeanno){
  //campo readonly
}

I do not understand what March is, but if you want to evaluate the previous if ...

(besides that algorithm will work for you only one year you should not use literal 2018 ..: ())

$anio = '2018';
$hoy = new \DateTime();
$findea = new \DateTime($anio.'-12-28'); // no hay que presuponer. No se cual es el ultimo dia. es 31 por ahora. :))))

$findea->modify('last day of this month');

if($hoy < $findea){
  //campo readonly
}
    
answered by 20.12.2018 в 17:56
0

Yes the current month is January, February or March will only show the current year and the previous year.

On the other hand, if the month is between April and December , it will show the current year only.

<?php

    $year = date("Y");
    $yearP = $year - 1;
    $yearN = $year + 1;
    $month = date("m");

    if ($month > 3) {
        $option = '
            <option value="' . $year . '" disabled>' . $year . '</option>
            <option value="' . $yearN . '">' . $yearN . '</option>
        ';
    }
    else {
        $option = '
            <option value="' . $yearP . '">' . $yearP . '</option>
            <option value="' . $year . '">' . $year . '</option>
            <option value="' . $yearN . '" disabled>' . $yearN . '</option>
        ';
    }

?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <select>
        <?= $option ?>
    </select>
</body>
</html>
    
answered by 20.12.2018 в 18:18