Skip to content

Instantly share code, notes, and snippets.

@LucianoCharlesdeSouza
Last active November 10, 2019 00:03
Show Gist options
  • Select an option

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

Select an option

Save LucianoCharlesdeSouza/d16cc00d285a2aa4137ba5b781a84c47 to your computer and use it in GitHub Desktop.
<?php
/**
* Class Calendar
*
* @author Luciano Charles de Souza
* E-mail: souzacomprog@gmail.com
* Github: https://github.com/LucianoCharlesdeSouza
* YouTube: https://www.youtube.com/channel/UC2bpyhuQp3hWLb8rwb269ew?view_as=subscriber
*/
class Calendar
{
public function month($month)
{
$months = array(
'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 deconhecido";
}
public function days($month)
{
$days = array(
'01' => 31, '02' => 28, '03' => 31, '04' => 30, '05' => 31, '06' => 30,
'07' => 31, '08' => 31, '09' => 30, '10' => 31, '11' => 30, '12' => 31
);
if (((date('Y') % 4) == 0 and (date('Y') % 100) != 0) or (date('Y') % 400) == 0) {
$days['02'] = 29;
}
return $days[$month];
}
public function week()
{
$days = "DSTQQSS";
$week = [];
for ($i = 0; $i < 7; $i++) {
$week[] = "<td>" . $days{$i} . "</td>";
}
return implode('', $week);
}
public function amountOfWeeks($month)
{
return jddayofweek(cal_to_jd(CAL_GREGORIAN, $month, "01", date('Y')), 0);
}
public function render($month)
{
$days = $this->days($month);
$nameMonth = $this->month($month);
$dayWeek = $this->week();
$amountOfWeeks = $this->amountOfWeeks($month);
return $this->makeTable($nameMonth, $days, $dayWeek, $amountOfWeeks);
}
private function makeTable($nameMonth, $days, $dayWeek, $amountOfWeeks)
{
$dayCurrent = 0;
$increment = 0;
$tableOpen = "<table style='text-align:center;'><tr><td style='font-size:1.4em;'>{$nameMonth}</td></tr><tr><td></td>{$dayWeek}</tr>";
$tableBody = '<tr>';
$tableClose = '</table>';
$tableBody .= $this->makeLineDays($increment, $dayCurrent, $days, $tableBody, $amountOfWeeks);
return $tableOpen . $tableBody . $tableClose;
}
private function makeLineDays($increment, $dayCurrent, $days, $tableBody, $amountOfWeeks)
{
for ($line = 0; $line < 6; $line++) {
$increment++;
if ($dayCurrent + 1 <= $days) {
$tableBody .= "<td style='background-color:#ccc; padding:5px;'>Semana {$increment}</td>";
for ($column = 0; $column < 7; $column++) {
if ($dayCurrent + 1 <= $days) {
$tableBody .= "<td style='width:30; height:30; background-color:#f3f3f3; padding: 5px'>";
if ($column < $amountOfWeeks && $line == 0) {
$tableBody .= ' ';
} else {
$tableBody .= ++$dayCurrent;
$tableBody .= "</td>";
}
} else {
break;
}
}
}
$tableBody .= "</tr>";
}
return $tableBody;
}
}
date_default_timezone_set('America/Sao_Paulo');
$calendar = new Calendar;
echo $calendar->render('11');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment