Know what day falls next Monday

4

I need a schedule for an agenda that will give me the date of the next Mondays from a certain date

Ex: today and day 06/01 I need to know what day the next 10 Mondays will fall.

1 = 05/06/2017
2 = 12/06/2017
3 = 19/06/2017
4 = 26/06/2017
5 = 03/07/2017
6 = 10/07/2017
8 = 17/07/2017
9 = 24/07/2017
10= 31/07/2017

My problem is to know the next Monday from a certain date, because the next one will be easier and only add up to 7 days.

    
asked by anonymous 01.06.2017 / 18:44

4 answers

6

Follow the sample code for the following Monday from a date:

$dia = new DateTime( '2017-06-01' );
$dia->modify( 'next monday' );

echo $dia->format('d/m/Y'); // 05/06/2017

Link to view the test

Get the next 10 seconds in an array from the next second by reference to the current day:

$dia = new DateTime();
$dia->modify( 'next monday' );

$nextMondaysNeed = range(1,10);
$nextMondaysArray = array($dia->format('Y-m-d'));

foreach($nextMondaysNeed as $number)
{
    $nextMondaysArray[] = $dia->modify('+7 day')->format('Y-m-d');
}

print_r($nextMondaysArray);

Follow preview link.

    
01.06.2017 / 18:52
1

If you use any version of php under 5.3, use strtotime ():

$dataStr = "2010-11-24";
$timestamp = strtotime($dataStr);
echo date("d", $timestamp);

If you use a version greater than or equal to 5.3, you can use this:

$string = "2010-11-24";
$date = DateTime::createFromFormat("Y-m-d", $string);
echo $date->format("d");

Ref: Stack

    
01.06.2017 / 19:00
1

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++; 
}

?>
    
01.06.2017 / 19:59
0

So that way it works in more programming languages.

// 1 SEGUNDA 
// 2 TERCA 
// 3 QUARTA 
// 4 QUINTA 
// 5 SEXTA 
// 6 SABADO 
// 7 DOMINGO

$diaDaSemana = 3; // ALTERE PELO DIA DA SEMANA DA DATA
$xx = (1 - diaDaSemana + 7) % 7;
echo date('d/m/Y', strtotime('+'.$xx.' days', strtotime('14-07-2014')));// ALTERE 14-07-2017 pela data a ser adicionado os dias

Date is next Monday.  if you want to change the next Tuesday

  $xx = (1 - diaDaSemana + 7) % 7;

by

  $xx = (**2** - diaDaSemana + 7) % 7;
    
01.06.2017 / 19:02