Skip to content

Instantly share code, notes, and snippets.

@wtorsi
Last active February 11, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtorsi/a741947acb287c653ac3bcf94b582ec3 to your computer and use it in GitHub Desktop.
Save wtorsi/a741947acb287c653ac3bcf94b582ec3 to your computer and use it in GitHub Desktop.
[Twig] display calendar by months using Twig
<div class="row">
{%- for month in months -%}
<div class="col-6">
<table class="table table-condensed table-calendar bg-white">
<thead>
<tr>
<th colspan="7">
{{- month|format_date('medium', 'MMM') -}}
</th>
</tr>
</thead>
<tbody>
{%- set delta = 1 -%}
{%- set dow = month|date_modify('first day of this month')|date('w') -%}
{%- set start = month|date_modify((dow >= delta ? delta - dow : dow - 7 + delta ) ~ 'days') -%}
{%- set weeks = month|date_modify('last day of this month')|date('W') - month|date_modify('first day of this month')|date('W') -%}
{%- for week in 0..weeks -%}
<tr>
{%- set current = start|date_modify('+' ~ (7 * week) ~ 'days') -%}
{%- for i in 0..6 -%}
{%- set day = current|date_modify('+' ~ i ~ ' day') -%}
{%- set disabled = day|date('n') != month|date('n') -%}
<td class="{{ disabled ? "disabled" }} ">
{{- day|date('d') -}}
</td>
{%- endfor -%}
</tr>
{%- endfor -%}
</tbody>
</table>
</div>
{%- endfor -%}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment