While, limit the result

Asked

Viewed 174 times

0

I would like to limit the result of while. My code displays the number of days between two dates correctly, but I would like the result (no while) to be showing the dates every 30 days, as if each line was a month, to facilitate counting.

Ex:

01-01-2017, 02-01-2017, ... completed 30 days enter
01-01-2017, 02-01-2017, ... completed 30 days enter
01-01-2017, 02-01-2017, ... completed 30 days enter

Just follow my code

<?php
include "exibe_ico.php";
if (empty($_POST['data_inicial']) or empty($_POST['data_final']) ) { echo "<script> window.alert('Preencha a data!'); history.back(); </script>"; exit(); }

ValidaData($_POST['data_inicial']);
ValidaData($_POST['data_final']);

$data_inicial = $_POST['data_inicial'];
$data_final = $_POST['data_final'];

function geraTimestamp($data) {
$partes = explode('/', $data);
return mktime(0, 0, 0, $partes[1], $partes[0], $partes[2]);
}

$time_inicial = geraTimestamp($data_inicial);
$time_final = geraTimestamp($data_final);

$diferenca = $time_final - $time_inicial; // 19522800 segundos
$dias = ((int)floor( $diferenca / (60 * 60 * 24))+1); // 225 dias

echo "<table border='1' width=80%>";
echo "<tr>";
echo "<th>Início</th>";
echo "<th>Fim</th>";
echo "<th>Nº dias</th>";
echo "</tr>";


echo "<tr>";
echo "<td align='center'>$data_inicial</td>";
echo "<td align='center'>$data_final</td>";
echo "<td align='center'>$dias</td>";
echo "</tr>";

echo "</table>";

echo "<p><div align=left><a href='dias.php?acao=entrar'><< Voltar</a></font></div>";

?>

<?php
$Dias = 0;
$contagem = 1;
$enter = 1;
$result = date("d-m-Y", strtotime(str_replace('/', '-', $data_inicial)));
while($Dias < $dias)
{
echo "
<table>
<tr>
<td width=60 align='center'>{$contagem}º dia</td>
<td width=100 align='center'>"; echo date("d-m-Y", strtotime("+ $Dias days",strtotime($result))); echo "</td>
</tr>
</table>
";
$Dias++;
$contagem++;

}
  • 1

    Ask a question correctly, tell how the date format is in the bank, and in the example all 3 lines are equal, you would like an answer with the same coin of your question?

1 answer

1

Try to get the timestamp month

$php_date = getdate($timestamp);
$mes = date("m", $timestamp); 

So every change of the month you create the line break.

  • 1

    I don’t get it, mate! I’m not getting a break in while (adding a enter when it shows 30 items can be without table. <td width=100 align='center'>"; echo date("d-m-Y", strtotime("+ $Days days days",strtotime($result)); echo "</td>

  • 1

    If you always give 30 lines you can make a counter and catch multiples of 30. But if there is a month with a record number other than 30 won’t work, then do you have to take the current month to know the time of the line break, or is your problem just in the layout? How to make a line break?

  • I will specify better, It is reported a period, example: 01/01/2017 to 01/08/2017 and the site says how many days were counted , in the above case were 213 days, including the 1st day, all well so far. Now the result is shown these days: 1st day: 01/01/2017

  • It is informed a period, example: 01/01/2017 to 01/08/2017 and the site says how many days were counted , in the above case were 213 days, including the 1st day Now in the result is shown these days: 1st day: 01/01/2017 2nd day: 01/01/2017 and so on Only that I want it to be shown like this: Layalt 01/01/2017, 01/02/2017, 01/03/2017 ... until you add 30 dates, and a Enter xx/xx/xxxx, xx/xx/xxxx, xx/xx/xxxx, ... until you add 30 dates, and a Ente , or I want these dates to be in line 30, no matter the month or day, it is number 30, no matter the beginning or end of the date

  • Do like this: if(($counter %30) == 0){ //Do line break }

  • I couldn’t. <? php $Days = 0; $count = 1; $enter = 1; $counter = 1; $result = date("d-m-Y", strtotime(str_replace('/', '-', $start_date)); //Validadata($result) while($Days < $days) { if(($counter %30) == 1) { echo " <table> <tr> <td width=100 align='center'>"; echo date("d-m-Y", strtotime("+ $Days days",strtotime($result)); echo "</td> </tr> </table> "; } $Days++;&#Xa$count++ ##a; ;}

  • You have to put it under ... </table>"; the if and the line break action, for example if(($counter % 30) == 0){ echo "<br><br>"; }, which so every time you give 30 items it will break the line.

  • So if(($counter % 30) == 30){ echo "<br>";} didn’t work out I put it like this if(($counter % 30) == 30){ echo "<br>";} Broke, but only once

  • I say So if(($counter % 30) == 0){ echo "<br>";} didn’t work So I put if(($counter % 30) == 10){ echo "<br>";} Broke, but only once How it works % 30

  • He takes the rest of the division, then every multiple of 30 he breaks the line.

  • Okay, brother worked out now. Thanks

  • Mark as the answer to the question, please.

  • How do I mark a response

  • It is a "V" that has under the "counter" of the answer

Show 9 more comments

Browser other questions tagged

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