Although the author already has an answer, I will put here my contribution to those who go through problems involving programming logic in PHP.
Name the cattle..
It is very common for programmers to name strange functions and variables, or words from another language. This makes it difficult to solve problems, often of the author of the code himself. So whenever possible, change $x
for $quantidadeAtual
... etc. Often the code can become more extensive, but the solution of problems and errors become much easier to identify, because of the good writing, which generates a good reading.
Procedural thinking
Often a problem because it is not well defined appears to be a programming problem (language resources, syntax, native functions) and it is not. Think 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, and not necessarily resources or additional knowledge. Except in cases where you do not completely master the 'palette', which is the basic one. Some PHP functions return in strings, which can make your life difficult at times. At the end of this post, I will show that it is possible to solve this problem in particular, without using the native functions of dates. Which shows that you’re not stuck with any method of development, and that you have 'n' ways of solving a problem. A good exercise, is to enumerate in a paper (even real paper), what steps you would do to solve a certain problem. That’s natural language! No PHP, C#, JAVA.. Good English itself.
Do not follow cake recipe
Not even this post is a cake recipe. There are situations that you will write the variable with strange names; quit implementing without a strategy; put loops like [$j][$i][$m]
or worse...and all these cases, at least at some point will be situations worth explaining. Someone may want to keep a code short of 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. It follows proposed solution, with extreme opposite 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++;
}
?>
You can also use the same logic as this question
– rray
You could better title to "Algorithm to find out next second" ?
– Paulo Sérgio Duff