<?php //, . , . if (isset($_REQUEST['date'])) { //, ... $pattern = "/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/"; if (preg_match($pattern, $_REQUEST['date'])) { $date = $_REQUEST['date']; } else { die(' '); } } else { $date=date("Ymd"); } $sd = explode("-", $date); $year = $sd[0]; $month = $sd[1]; $day = $sd[2]; // $dayofmonth = date('t', mktime(0, 0, 0, $month, 1, $year)); // $todate = "$year-$month-$dayofmonth"; $fromdate = "$year-$month-01"; $query = "SELECT date,enddate from sobytia WHERE startdate<='$todate' AND enddate>=$fromdate"; $res_db = $db->sql($query);
$d = array();$k=array(); for($i = 1; $i<=$dayofmonth; $i++){ $k[$i] = $i; } $i=0; while ($a = mysqli_fetch_row($res_db)) { //for($i = 1; $i<=$dayofmonth; $i++){ foreach ($k as $i) { // 0 if($i<10) $cd = "$year-$month-0".$i; else $cd = "$year-$month-$i"; if ($cd >= $a[0] && $cd <= $a[1]) { $d[$i] = $cd; unset($k[$i]); } } }
// $day_count = 1; // 1. $num = 0; for($i = 0; $i < 7; $i++) { // $dayofweek = date('w', mktime(0, 0, 0, $month, $day_count, $year)); // 1 - , ..., 6 - $dayofweek = $dayofweek - 1; if($dayofweek == -1) $dayofweek = 6; if($dayofweek == $i) { // , // $week // $week[$num][$i] = $day_count; $day_count++; } else { $week[$num][$i] = ""; } } // 2. while(true) { $num++; for($i = 0; $i < 7; $i++) { $week[$num][$i] = $day_count; $day_count++; // - // if($day_count > $dayofmonth) break; } // - // if($day_count > $dayofmonth) break; } // 3. $week // // echo '<table id="calendar">'; // $rusdays = array('','','','','','',''); $rusmonth = array('','','','','','','','','','','',''); echo '<thead> <tr> <td onclick="monthf(\'prev\');"><</td> <td colspan="5">'.$rusmonth[$month-1].', '.$year.'</td> <td onclick="monthf(\'next\');">></td> </tr>'; echo '<tr>'; foreach ($rusdays as $rusday){ echo '<td>'.$rusday.'</td>'; } echo '</tr>'; echo '</thead>'; // for($i = 0; $i < count($week); $i++) { echo "<tr>"; for($j = 0; $j < 7; $j++) { if(!empty($week[$i][$j])) { // ee if($week[$i][$j]==$day) { echo '<td class="today">'; } else { echo '<td>'; } // , if($d[$week[$i][$j]]) { echo '<a href="/afisha/'.$d[$week[$i][$j]].'/">'.$week[$i][$j].'</a>'; } else { echo $week[$i][$j]; } echo '</td>'; } else echo "<td> </td>"; } echo "</tr>"; } ?>
<script> var mon = parseInt("<?php echo $month; ?>"); var year = parseInt(<?php echo $year; ?>); function monthf(pn){ if (pn == 'next'){ mon++; }else if (pn == 'prev'){ mon--; }else{ alert(' '); return false; } if (mon > 12){ year ++; mon = 1; } if (mon < 1){ year --; mon = 12; } if ((mon < 10) && (mon >= 1)){ mon = '0'+mon; } var nextDate = year+'-'+mon+'-00'; var ajaxaddr = "___?date='+nextDate; var http = new XMLHttpRequest(); if (http) { http.open('get', ajaxaddr); http.onreadystatechange = function () { if(http.readyState == 4){ if (http.status == 200) { document.getElementById('calendar').innerHTML = http.responseText; } } } http.send(null); } } </script>
<?php echo "</table>"; ?>
Source: https://habr.com/ru/post/236813/
All Articles