Calculate days on a given date per month

1

Hello, I would like to reflect the number of days of an absent worker from his date of absence either using Carbon or something native in php, use Laravel the example is this:

In the BD I consult the absenteeism that said worker has

$ausentismo_anual_dias = $empresa->ausentismo()
->whereYear('fecha_ausente', Carbon::now()->format('Y'))
->get();

In a foreach cycle I want to print from your date absence example

foreach($ausentismo_anual_dias as $row){
           //Aqui quiero calcular los dias de ausencias 

        }

Note days of absence must be calculated by month example This worker is absent from the given date 2018-10-06 (Month October) to the end date of the month October 2018-10-31 has 25 days absent, then we go to November 2018-11-01 to the current date 2018-11-05 you have that 5 days elapsed > All this must be covered from the cycle

showing it like this:

October: 25 days absent

November: 5 days absent

December should not show since we are not in that month so it should be in 0 days

I hope you can help me with this request

    
asked by vdjkelly 06.11.2018 в 04:18
source

2 answers

0

You must generate the dates that are within the range of the absences. You can do something like the following:

/**
 * genera fechas entre 2 fechas y las agrupa por periodo (año-mes)
 *
 * si until es nulo, se usa la fecha actual
 *
 * @param   string      $since   fecha desde con formato YYYY-mm-dd
 * @param   string|null $until   fecha hasta con formato YYYY-mm-dd
 * @return  array
 *
 */
function generateDates(string $since, string $until = null) {
    $dates = [];

    if (! $until) {
        $until = date('Y-m-d');
    }

    $since = strtotime($since);
    $until = strtotime($until);

    do {
        $period           = date('Y-m', $since); // para agrupar por periodo AÑO-MES
        $dates[$period][] = date('Y-m-d', $since);
        $since            = strtotime("+ 1 day", $since);
    } while($since <= $until);

    return $dates;
}

to execute:

$dates = generateDates('2018-10-06', '2018-11-05');
// print_r(generateDates('2018-10-06'));
// Si no pasas el segundo parámetro se asume que es la fecha actual

To show the days for each period you must go through the arrangement generated by the function ex:

foreach ($dates as $period => $date) {
    $days = count($date);
    echo "El periodo {$period} tiene {$days} días <br>";
}

/* output
El periodo 2018-10 tiene 26 días 
El periodo 2018-11 tiene 5 días
*/

I print the outputs of what the function generates:

print_r($dates);
/** output:
Array
(
[2018-10] => Array
    (
        [0] => 2018-10-06
        [1] => 2018-10-07
        [2] => 2018-10-08
        [3] => 2018-10-09
        [4] => 2018-10-10
        [5] => 2018-10-11
        [6] => 2018-10-12
        [7] => 2018-10-13
        [8] => 2018-10-14
        [9] => 2018-10-15
        [10] => 2018-10-16
        [11] => 2018-10-17
        [12] => 2018-10-18
        [13] => 2018-10-19
        [14] => 2018-10-20
        [15] => 2018-10-21
        [16] => 2018-10-22
        [17] => 2018-10-23
        [18] => 2018-10-24
        [19] => 2018-10-25
        [20] => 2018-10-26
        [21] => 2018-10-27
        [22] => 2018-10-28
        [23] => 2018-10-29
        [24] => 2018-10-30
        [25] => 2018-10-31
    )

[2018-11] => Array
    (
        [0] => 2018-11-01
        [1] => 2018-11-02
        [2] => 2018-11-03
        [3] => 2018-11-04
        [4] => 2018-11-05
    )

)
*/
    
answered by 06.11.2018 / 04:51
source
0
@forelse ($ausentismo_anual_ene as $key => $row)
                                @php
                                    $period =    new DatePeriod(
                                    new DateTime($row['fecha_ausente']),
                                    new DateInterval('P1D'),
                                    new DateTime($row['fecha_alta']));
                                    foreach ($period as $key => $value)
                                    {
                                        $dias[] = $value->format('Y-m-d');
                                    }
                                $comprobar = new App\Helpers\ComprobarDiasAusente();
                                @endphp
                                        @foreach ($dias as $key => $dia)
                                            @php
                                                $dates = $comprobar->generateDates($dia);
foreach ($dates as $period => $date) {
    $days = count($date);
    echo "El periodo {$period} tiene {$days} días <br>";
}

                                            @endphp

                                        @endforeach

                                        @php

                                        @endphp
                                    @empty
                                        0,
                                    @endforelse

I managed to print it but I repeated the days in this way

El periodo 2018-10 tiene 16 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 15 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 14 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 13 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 12 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 11 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 10 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 9 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 8 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 7 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 6 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 5 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 4 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 3 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 2 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-10 tiene 1 días <br>El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-11 tiene 6 días <br>
                                                                                    El periodo 2018-11 tiene 5 días <br>
                                                                                    El periodo 2018-11 tiene 4 días <br>
                                                                                    El periodo 2018-11 tiene 3 días <br>
                                                                                    El periodo 2018-11 tiene 2 días <br>
                                                                                    El periodo 2018-11 tiene 1 días <br>
    
answered by 06.11.2018 в 13:43