Skip to content

Instantly share code, notes, and snippets.

@Faks
Forked from Vinze/calendar.php
Last active October 2, 2018 16:06
Show Gist options
  • Save Faks/f73def848c3d1e00222d0a54493a00ad to your computer and use it in GitHub Desktop.
Save Faks/f73def848c3d1e00222d0a54493a00ad to your computer and use it in GitHub Desktop.
Create an HTML calendar with PHP and Carbon
<?php
public static function Render()
{
#Sets Current Date
$dt = Carbon::createFromFormat('Y,m,d', Date('Y,m,d'));
// Make sure to start at the beginning of the month
$dt->startOfMonth();
// Set the headings (weeknumber + weekdays)
$headings = [ 'M', 'T', 'W', 'T', 'F', 'S', 'S']; #translation enhancement
// Create the table
$calendar = '<table class="calendar" id="wp-calendar">';
$calendar .= '<caption>'.$dt->format('F Y').'</caption>';
$calendar .= '<tr>';
// Create the calendar headings
foreach ($headings as $heading)
{
$calendar .= '<th class="header">'.$heading.'</th>';
}
// Day of week isn't monday, add empty preceding column(s)
if ($dt->format('N') != 1)
{
$calendar .= '<td colspan="'.($dt->format('N') - 1).'">&nbsp;</td>';
}
// Get the total days in month
$daysInMonth = $dt->daysInMonth;
// Go over each day in the month
for ($i = 1; $i <= $daysInMonth; $i++)
{
// Monday has been reached, start a new row
if ($dt->format('N') == 1)
{
$calendar .= '</tr><tr>';
}
// Append the column
$calendar .= '<td class="day" rel="'.$dt->format('Y-m-d').'">';
if ($dt->day == Carbon::now()->format('d') )
{
#Active Date Today
$calendar .= "<a>" . $dt->day . '</a>';
}
else
{
#Not Active Dates
$calendar .= $dt->day;
}
$calendar .= '</td>'; // Increment the date with one day
$dt->addDay();
}
// Last date isn't sunday, append empty column(s)
if ($dt->format('N') != 7)
{
$calendar .= '<td colspan="'.(8 - $dt->format('N')).'">&nbsp;</td>';
}
// Close table
$calendar .= '</tr>';
$calendar .= '</table>';
// Return calendar html
return $calendar;
}
echo MyClass::Render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment