Skip to content

Instantly share code, notes, and snippets.

@LucianoCharlesdeSouza
Created June 21, 2019 16:16
Show Gist options
  • Select an option

  • Save LucianoCharlesdeSouza/6a0aa10ff307b32c192a9212d79ced4a to your computer and use it in GitHub Desktop.

Select an option

Save LucianoCharlesdeSouza/6a0aa10ff307b32c192a9212d79ced4a to your computer and use it in GitHub Desktop.
Classe geradora de uma ou mais tabelas em formato de Calendario
<?php
class Calender
{
private function createWeeks()
{
$weeks = "DSTQQSS";
$td = '';
for( $i = 0; $i < 7; $i++ ){
$td .= "<td>{$weeks{$i}}</td>";
}
return $td;
}
private function getNumberDays($month)
{
$numberDays = [
'01' => 31, '02' => 28, '03' => 31, '04' =>30, '05' => 31, '06' => 30,
'07' => 31, '08' =>31, '09' => 30, '10' => 31, '11' => 30, '12' => 31
];
// altera o numero de dias de fevereiro se o ano for bissexto
if (((date('Y') % 4) == 0 and (date('Y') % 100)!=0) or (date('Y') % 400)==0){
$numberDays['02'] = 29;
}
return $numberDays[$month];
}
private function getNamemonth($month)
{
$months = [
'01' => "Janeiro", '02' => "Fevereiro", '03' => "Março",
'04' => "Abril", '05' => "Maio", '06' => "Junho",
'07' => "Julho", '08' => "Agosto", '09' => "Setembro",
'10' => "Outubro", '11' => "Novembro", '12' => "Dezembro"
];
if( $month >= 01 && $month <= 12){
return $months[$month];
}
return "Mês desconhecido";
}
public function makeCalender($month)
{
// retorna o número de dias que tem o mês desejado
$numberDays = $this->getNumberDays($month);
$nameMonth = $this->getNamemonth($month);
$currentDay = 0;
// função que descobre o dia da semana
$dayWeek = jddayofweek( cal_to_jd(CAL_GREGORIAN, $month,"01",date('Y')) , 0 );
$table = "<table border='0' cellspacing= 0' align='center'>";
$table .= '<tr>';
$table .= "<td colspan='7'><h3>{$nameMonth}</h3></td>";
$table .= '</tr>';
$table .= '<tr>';
// função que mostra as semanas aqui
$table .= $this->createWeeks();
$table .= '</tr>';
for( $tr = 0; $tr < 6; $tr++ ){
$table .= '<tr>';
for( $td = 0; $td < 7; $td++ ){
$table .= "<td width='30' height='30' ";
if( ($currentDay == ( date('d') - 1) && date('m') == $month) ){
$table .= " id='dia_atual' ";
}else{
if(($currentDay + 1) <= $numberDays ){
if( $td < $dayWeek && $tr == 0){
$table .= " id='dia_branco' ";
}else{
$table .= " id='dia_comum' ";
}
} else{
$table .= " ";
}
}
$table .= " align='center' valign='center'>";
/* TRECHO IMPORTANTE: A PARTIR DESTE TRECHO É MOSTRADO UM DIA DO CALENDÁRIO (MUITA ATENÇÃO NA HORA DA MANUTENÇÃO) */
if( $currentDay + 1 <= $numberDays ){
if( $td < $dayWeek && $tr == 0){
$table .= " ";
}else{
// echo "<input type = 'button' id = 'dia_comum' name = 'dia".($diacorrente+1)."' value = '".++$diacorrente."' onclick = "acao(this.value)">";
$table .= "<a href='".$_SERVER["PHP_SELF"]."?mes=$month&dia=".($currentDay+1)."'>".++$currentDay . "</a>";
}
}else{
break;
}
/* FIM DO TRECHO MUITO IMPORTANTE */
$table .= '</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
public function makeCalenderComplete()
{
$table = "<table align='center'>";
$cont = 1;
for( $j = 0; $j < 4; $j++ ){
$table .= "<tr>";
for( $i = 0; $i < 3; $i++ ){
$table .= "<td>";
$table .= $this->makeCalender( ($cont < 10 ) ? "0".$cont : $cont );
$cont++;
$table .= "</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}
}
/**
* Modo de uso
*/
$calender = new Calender;
echo $calender->makeCalender('07');
echo "<br/>";
echo $calender->makeCalenderComplete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment