Last active
February 4, 2016 17:08
-
-
Save harentius/8e4c8527f0afc8344b38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Форма --> | |
<h3>Выбрать нужные дни недели из периода: 01.01.2016 - 31.01.2016</h3> | |
<form action="<?php $_SERVER['PHP_SELF'] ?>" method = "post"> | |
<label>Пн.<input type="checkbox" name="input[]" value="1"></label> | |
<label>Вт.<input type="checkbox" name="input[]" value="2"></label> | |
<label>Ср.<input type="checkbox" name="input[]" value="3"></label> | |
<label>Чт.<input type="checkbox" name="input[]" value="4"></label> | |
<label>Пт.<input type="checkbox" name="input[]" value="5"></label> | |
<button type='submit'>Посчитать количество всех дней</button> | |
</form> *********************************************************************<br/><br/> | |
<!-- Oбработчик --> | |
<?php | |
function isWeekend(\DateTime $dateTime, array $weekendDates = array()) | |
{ | |
foreach ($weekendDates as $weekendDate) { | |
if ($weekendDate === $dateTime->format('d-m-Y')) { | |
return true; | |
} | |
} | |
if (isset($_POST['input'])) | |
{ | |
/* Период дней */ | |
$dateStart = new DateTime( '01-01-2016' ); // Дата начала переода | |
$dateEnd = new DateTime( '31-01-2016' ); // Дата окончания переода | |
echo "Дата начала периода: " . $dateStart->format( 'd-m-Y') . "<br/>"; | |
echo "Дата окончания периода: " . $dateEnd->format( 'd-m-Y') . "<br/><br/>"; | |
$daysStudy = $_POST['input']; // Массив в который заносятся выбранные дни недели из формы. 0 - это Вс. 1- Пн. и т.д. | |
$allDaysStudy = 0; | |
$sql = mysql_query("SELECT *, DATE_FORMAT(`date_free`, '%d-%m-%Y') AS date_f FROM `freedays` "); | |
$weekendDates = []; | |
while ($row = mysql_fetch_array($sql)) { | |
$weekendDates[] = $row['date_free'] | |
} | |
while ( $dateStart <= $dateEnd ) | |
{ | |
if ( in_array( $dateStart->format( 'w' ), $daysStudy ) && !isWeekend($dateStart, $weekendDates))// Перебираю и подсчитываю сумму всех дней | |
$allDaysStudy++; | |
$dateStart-> modify( '+1 day' ); | |
} | |
echo "Количество всех выбраных дней недели в заданном периоде дат: <b>", $allDaysStudy, "</b> дня \n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment