Although the author already has an answer, I will put here my contribution to anyone who goes through problems involving programming logic in PHP.
Name the cattle ..
It is very common for programmers to put names of strange functions and variables, or words from another language. This makes troubleshooting difficult, often by the code's own author. Then, whenever possible, change $x
to $quantidadeAtual
... etc. Often the code can get longer, but the problem solving and errors are much easier to identify, because of good writing, which generates a good reading.
Think procedural
Often, a problem because it is not well defined, appears to be a programming problem (language features, syntax, native functions) and is not. Think that you are a painter, and the language features are your color palette. Maybe you just need to be a little creative to solve a problem, not necessarily resources or additional knowledge. Except in cases where you do not master the 'palette' completely, which is the basics. Some PHP functions return in strings, which can make your life difficult sometimes. At the end of this post, I'll show you that you can solve this problem in particular, without using the native date functions. Which shows that you are not tied to any method of development, and that you have 'n' ways to solve a problem. A good exercise, is to list on a paper (even really paper), what steps you would take to solve a problem. This in natural language indeed! Nothing PHP, C #, JAVA .. The good Portuguese anyway.
Do not follow cake recipe
Not even this post is a cake recipe. There are situations when you write the variable with strange names; will implement without a strategy; will put loops of type [$j][$i][$m]
or worse ... and all these cases, at least at some point are worthy of explanation. Someone may want to keep a short code from a loop, have copied and pasted code, or used someone else's class, etc. The point here is, programming logic is always the way, regardless of your method of implementing. Here is a proposed solution with opposite extreme examples :
<?php header ('Content-type: text/html; charset=UTF-8');
$limiteDoMes = 31; // Essa linha estabelece um limite de dias para o mês
$agenda = array(12, 13, 17, 21, 29); // Array que guarda todos os dias agendados
$diasAgendados = count($agenda); // Conta quantos dias tem agendado, para aplicação saber quantas vezes terá que fazer sua tarefa até parar
$diasCorridos = 0; // Estabelece uma partida para a tarefa ser realizada
while ($diasCorridos < $diasAgendados) // Enquanto (while) a quantidade de dias corrigos na análise, for menor que a quantidade de dias agendados, faça a tarefa abaixo
{
$proximaSegunda = $agenda[$diasCorridos] + 7; // Calcula a proxima segunda de cada dia agendado
$segundaDoMesSeguinte = $agenda[$diasCorridos] + 7 - $limiteDoMes; // Calcula a proxima segunda se cair no mês seguinte
if ($proximaSegunda > $limiteDoMes) // Se a próxima segunda cair no mês seguinte, escreva esse texto
{
echo "A próxima segunda feira depois do dia $agenda[$diasCorridos] será no mês seguinte, do dia $segundaDoMesSeguinte ";
}
else{ // Caso contrário, se cair neste mês, escreva isso
echo "A próxima segunda feira depois do dia $agenda[$diasCorridos] será $proximaSegunda<br>";
}
$diasCorridos++; // Depois da tarefa executada, adicione 1, a dias corrigos, e faça a tarefa de novo, como uma roleta
}
?>
Version of the same solution in traditional coding
<?php
header('Content-type: text/html; charset=UTF-8');
$lm = 31;
$agenda = array(12,13,17,21,29);
$da = count($agenda);
$dc = 0;
while ($dc < $da)
{
$ps = $agenda[$dc] + 7;
$sms = $agenda[$dc] + 7 - $lm; seguinte
if ($ps > $lm)
{
echo "A próxima segunda feira depois do dia $agenda[$dc] será no mês seguinte, do dia $sms ";
} else {
echo "A próxima segunda feira depois do dia $agenda[$dc] será $ps<br>";
}
$dc++;
}
?>