How to pick the right date for the next event

Asked

Viewed 47 times

0

I have a list of days of the week that shows when an event should occur and another variable with the interval that should occur that event, for example, if in the list is 0,1,0,1,0,1,0 [seg-qua-sex] the events will occur, and if in my interval variable I have the value of 2 this means that it should occur week yes week no.

What I need to know is when the next event will occur, does anyone have any idea how I can do it, my problem is much more complex, but I tried to make it as simple as possible.

2 answers

1

In a procedural way, only with the information of the question:

$intervalo = 2; //2 em 2 semanas
$semanaAtual = array(0,0,1,0,0,0,0); //domingo, segunda, terça, quarta, quinta, sexta, sábado
$semanaProxima = array(0,0,0,1,0,0,0); //domingo, segunda, terça, quarta, quinta, sexta, sábado

$ultimoEvento = '2016-02-23'; //string contendo a data do último evento
$diaDaSemana = date("w", strtotime($ultimoEvento)); //dia da semana em que ocorreu o último evento
$dataProximoEvento = ''; //data do próximo evento

//verifica se dentro da própria semana ainda ocorrerá o evento
for($i=$diaDaSemana+1; $i<=6; $i++) {
    if($semanaAtual[$i]) {
        $dataProximoEvento = date("Y-m-d", strtotime($ultimoEvento . ' + ' . ($i-$diaDaSemana) . ' days'));
        break;
    }
}

//verifica qual o próximo dia do evento na próxima semana de acordo com o intervalo
if(empty($dataProximoEvento)) {
    $primeiroDiaSemana = new DateTime(date('Y-m-d', strtotime("last Sunday", strtotime($ultimoEvento . ' + ' . (7*$intervalo) . ' days') )));

    for($i=0; $i<=6; $i++) {
        if($semanaProxima[$i]) {
            $dataProximoEvento = date('Y-m-d', strtotime($primeiroDiaSemana->modify("+$i days")->format("Y-m-d H:i")));
            break;
        }
    }
}

echo $dataProximoEvento;

0

I managed to solve the problem as follows

<?php
$regra_frequencia = '0,1,0,1,0,1,0'; 
$dow = explode(",", $regra_frequencia);
$dow_now = date('w');
$dow_repeat = array();
$intervalo = 2;

foreach($dow as $w => $on) {    
    if($on == 1) {
        $dow_repeat[] = $w;
    }
}

$dow_get = $dow_repeat[0];

if($dow_now < 6) {  
    foreach($dow_repeat as $d) {
        if($d > $dow_now) {
            $dow_get = $d;
            break;          
        }       
    }
}

$date = new DateTime(date('Y-m-d'));

if($dow_get > $dow_now) {
    $diff = $dow_get - $dow_now;
    $date->add(new DateInterval("P" . $diff . "D"));
    $dt_next_event = $date->format('Y-m-d');
} else {
    $diff = $dow_now - $dow_get;
    $date->sub(new DateInterval("P" . $diff . "D"));
    $date->add(new DateInterval("P" . $intervalo . "W"));
    $dt_next_event = $date->format('Y-m-d');    
}

echo $dt_next_event;
  • 1

    What if the frequency is different for each week? What if in the same week the event still occurs? Or was this not to be considered? Look at the answer I posted.

  • Opa Flilipe, thanks, but the frequency is the same always, if it is different will be another event, and in the case I did, so I tested the user can occur the event several times in the week, as in the example, mon, qua and sex. I wonder if he found some mistake?

  • In the example I posted too, you can have the same event in the same week, just change the arrays.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.