2

I have the following code that will output the UNAVAILABLE (previously booked) times of a Target Date. How would i edit this to output (in 30 minute increments) only the AVAILABLE times with Solspace Calendar?

{% set targetDate = "2016/09/27" %}
{% set calendarHandle = "default" %}
{% set day = craft.calendar.day({
     date: targetDate,
     calendar: calendarHandle
}) %}

{% for event in day.events %}
    {{ event.startDate.format('g:i a') }} - {{ event.endDate.format('g:i a') }}
{% endfor %}

1 Answer 1

5

Edited to provide a cleaner solution:

This code will display different contents for available times and unavailable times in 30 min increments, and makes no assumption on how long your events are.

{% set targetDate = "today" %}

{% set events = craft.calendar.events({
    dateRangeStart: targetDate,
    dateRangeEnd: targetDate
}) %}


{% for hour in craft.calendar.day({date: targetDate}) %}
    {% set dateFirstHalfHour = hour.date.copy.addMinutes(30) %}
    {% set dateSecondHalfHour = hour.date.copy.addMinutes(60) %}

    {% set hasEventsInFirstHalf = false %}
    {% set hasEventsInSecondHalf = false %}
    {% for event in events %}
        {% if hour.date >= event.startDate and hour.date < event.endDate %}
            {% set hasEventsInFirstHalf = true %}
        {% endif %}

        {% if dateFirstHalfHour >= event.startDate and dateFirstHalfHour < event.endDate %}
            {% set hasEventsInSecondHalf = true %}
        {% endif %}
    {% endfor %}

    <div>
        {{ hour.date.format("H:i") }} - {{ hasEventsInFirstHalf ? "unavailable" : "available" }}
    </div>
    <div>
        {{ dateFirstHalfHour.format("H:i") }} - {{ hasEventsInSecondHalf ? "unavailable" : "available" }}
    </div>

{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.