Timber Twig - show elements if date/time is in the present/future - twig

Two part question:
One:
I've used ACf to make some fields for linking to a Zoom meeting, including a date field and a time field, and I want a banner to only show if the date value is equal to or greater than the current date (i.e. today or future) - I've tried:
{% if post.meta('zoom_meeting_date') >= 'now'|date %}
<!-- markup here -->
{% endif %}
This is only working if the date is in the future, not if it is today.
Two: I want a 'join' button that will only show once it is within say 15mins of the start time - no idea where to start with that one!
Any help greatly appreciated!

I suggest using the same date filter without time on both sides of the comparison: |date('m/d/Y')
{% if date("2020-03-30 23:59:59.000000")|date('m/d/Y') >= 'now'|date('m/d/Y') %}
<h1>bang<h1>
{% endif %}
{% if date("2020-03-30 00:00:01.000000")|date('m/d/Y') >= 'now'|date('m/d/Y') %}
<h1>boom<h1>
{% endif %}
{{ 'now'|date }}
{{ 'now'|date('m/d/Y') }}

Related

How to calculate the composite's elements and get total of them?

Hi I have a composite that has a QTY field and a TOTAL PRICE field and the table below would calculate and display the subtotal. It works properly when there is only one row of composite, but when I add more items, the subtotal field displays two subtotals instead of one as a whole. I want the subtotal field to display 24 instead of 4 and 20. How can twig solve this implementation? In my SUBTOTAL, I have
{% for item in data.item %}
{% set total_qty = (item.qty)|number_format(2,'.',',') %}
{% set per_price = (item.total)|number_format(2,'.',',') %}
{% set net_cost = (total_qty * per_price )|number_format(2,'.',',') %}
{{ net_cost }}
{% endfor %}
Here is the screenshot to give you better understanding
Don't output the net cost inside the for loop.
First create the sum, then display it after the loop.
Also don't use number_format before the final result.
{% set net_cost = 0 %}
{% for item in data.items %}
{% set net_cost = nest_cost + item.qty * item.total %}
{% endfor %}
{{ net_cost|number_format(2, '.', ',') }}
demo

How can i query entries by a field that are within a matrix?

Each entry has a matrix field with fields including start date, end date, location etc
I need to list and display only the entries after now.
I am able to filter them by
{% set queryEntries = craft.entries({
section: ['events'],
}) %}
{% for entry in queryEntries.all() %}
{% if entry.eventDetails.dateFrom|date("U") > "now"|date("U") %}
{{ entry.title }}
{% endif %}
{% endfor %}
This isn't great though as when the events list grows it won't be very performant and I have entries that I don't want.
Am I am able to filter the queryEntries by entry.eventDetails.dateFrom|date("U") > "now"|date("U") ?

How can I check the value of a dropdown menu in craft cms / twig?

I have a dropdown menu with 2 options
Ordered
Unordered
I want to check if the value is Unordered or Ordered
{% set viewModel = {
cards: content['cards'] ?? [],
cardListType: content.cardListType ?? ''
} %}
{{ viewModel.cardListType|json_encode() }}
Returns this to me
{"0":{"label":"Unordered","value":"unordered","selected":true}}
I want to check if the value of viewModel.cardListType is unordered and that it's selected.
I have tried
{% if viewModel.cardListType.value is unordered %}
<p>UNORDERED</p>
{% endif %}
and
{% if viewModel.cardListType.value:unordered %}
<p>UNORDERED</p>
{% endif %}
and several other variations of this.
I don't understand the documentation/ syntax for dropdowns in craft or twig.
Can someone please explain like I'm 5 how this works and how to check the value?
I am using Craft 3 if this changes the syntax
Thanks in advance

Symfony2. Twig: Doesn't work logic function when date variable changes

{% set event_date = event.schedule|date('d-m-y') %}
{% set nowdate = "now"|date('d-m-y') %}
{% if event_date < nowdate %}
view some 1
{% else %}
view some 2
{% endif %}
If change event_date bigger or lower than nowdate, have one result: view some 2.
Why doesn't work?
You are in a string comparison situation. For better approach this, use the U filter as follow:
{% set event_date = event.schedule|date('U') %}
{% set nowdate = "now"|date('U') %}
{% if event_date < nowdate %}
view some 1
{% else %}
view some 2
{% endif %}
{{ event_date }}
Live testing in this fiddle.
Hope this help
You're comparing strings. d-m-y is not a usable format for comparing dates. Try Y-m-d, where the parts are in order of importance.
Even better, you could compare two \DateTime objects instead of strings. I assume event.schedule is one, you just need to pass one for the current date to twig.
In addition to the date filter, Twig defines a date() function which has been designed precisely to compare dates.

Twig date loop each day in a period

So, here's my situation, I have a beginning date and an ending date, with Twig I want to be able to loop through all days in the so called period so that I could print out every day. Of course, that's just for understanding how to do it, the goal is to get them into a chart. Anyway, I have the following code (with what are my vars) :
{% set start_year = date(start) | date('d-m-Y') %}
{% set end_year = date(end)| date('d-m-Y') %}
{% for i in start_year..end_year %}
{{ i }}
{% endfor %}
My start var is 01-01-2003 and my end var is 10-05-2014. The values don't matter as they could change, but that's the format I have.
This actually prints out 0 1 which I don't understand at all. If anyone has an idea either how to do this or how to fix what I'm doing, it would be really nice. Thanks.
You cannot define a range of exact dates (neither as range in php) but you can create a range of seconds with step of a 24 hours second which is 86400; if you use date('U') it will convert date string to seconds since the Unix Epoch (same as Time() in php)
{% set start_date = '01-06-2014' %}
{% set end_date = '05-06-2014' %}
{% for x in range(start_date|date('U'), end_date|date('U'), 86400 ) %}
{{ x|date('d/m/Y') }}<br>
{% endfor %}
Tip
Pay attention the format of date to use - as separator not / because it will lead to totally different result
{% set pd_begdate = header_data.pd_begdate %}
{% set pd_enddate = header_data.pd_enddate ~ ' 23:59:59' %}
{% for key, count_item in range(pd_begdate|date('z'), pd_enddate|date('z'), 1)|slice(column_offset, column_count, 'preserve_keys') %}
{% set item = pd_begdate|date_modify('+'~ key ~' day') %}
<td class="tr5 td9"><p class="p9 ft0">{{ item|date("M") }}<br>{{ item|date("d") }}<br>{{ item|date("D") }}<br>{{ key + 1 }}</p></td>
{% endfor %}
This solution works for day light savings.
I had trouble with the solution when I tried to iterate over an entire year - after about 8 months, I found it would drop a day.
But based on the solution How to add n-days to twig date format within a for-loop?, I found that this would work:
{% set yearstart = "01-01-2003"|date('m-d-Y') %}
{% set eachday = "" %}
{% for i in range(0,365) %}
{% set eachday = yearstart|date_modify("+" ~ i ~ "day")|date('m-d-y') %}
{% endfor %}

Resources