Skip to content

Instantly share code, notes, and snippets.

@peteeveleigh
Created March 18, 2021 17:22
Show Gist options
  • Save peteeveleigh/fde73a37776ff60dec01799caafed21d to your computer and use it in GitHub Desktop.
Save peteeveleigh/fde73a37776ff60dec01799caafed21d to your computer and use it in GitHub Desktop.
Twig for figuring out number of days between 2 dates minus weekends/holidays
{% set start = '2020-01-01' %}
{% set end = '2021-03-17' %}
{% set holidays = ['2020-12-25', '2020-12-26', '2021-01-01'] %}
{% set difference = date(end).diff(date(start)) %}
{% set count = 0 %}
{% for i in 0..difference.days %}
{% set thisDay = start|date_modify(i ~ 'days') %}
{% set count = count + ((thisDay|date('D') not in ['Sun','Sat']) and (thisDay|date('Y-m-d') not in holidays)) %}
{% endfor %}
There are {{ count }} days between {{ start|date('jS F Y') }} and {{ end|date('jS F Y') }} not including weekends and holidays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment