Why is the date object in twig 1 day behind? - twig

I am using this in my template. The date of this post is 4/18/2018
twig
{% set today = "now"|date("m/d/Y") %}
{{ today }} // 04/17/2018
I've tried setting the timezone explicitly as well.
{% set today = "now"|date("m/d/y", "America/Chicago") }}
{{ today }} // 04/17/2018
If I try to get the hour:
{% set currentHour = today|date('H') %}
{{ currentHour }} // 00
Is there anything else that would be causing the date to be so far off?
Thank you for any suggestions!

You are wrong about the fact that the date filter return a DateTime object, in fact the date filter just returns a formatted string
{% set today = "now"|date("m/d/y", "America/Chicago") }}
{{ dump(today) }} {# string(8) "04/19/18" #}
In order to solve your problem you would need to use the date function in twig
{% set today = date("NOW", "America/Chicago") %}
{{ dump(today) }}{# object(DateTime)#919 (3) { ["date"]=> string(19) "2018-04-19 00:32:04" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "America/Chicago" } #}
{{ today | date('H', false) }} {# false = ignore default set timezone #}{# 07 #}
Small insight as why your hours as so far off
it's because the date filter will try and do it's best to convert any string in a valid datetime string, thus meaning, because today contains the string 04/17/2018, that twig will convert the string to 04/17/2018 00:00:00 to create a valid datetime string
When omitting the timezone, twig will fall back to it's default timezone
In order to omit the false parameter in my example u would need to set the default the timezone in twig. This can be achieved with
$twig->getExtension('Twig_Extension_Core')->setTimezone('Europe/Paris');

Related

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

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') }}

How to add n-days to twig date format within a for-loop?

I'm working with twig and got the date and format working. I have a start date (let's say todays day) and I'd like to print every day into a table cell.
I have my date field in the var datum and I'm able to add 1 day with this. it's working.
{% set datum = date(current_user.cwmon)|date_modify("+1 day")|date('D d.m.y') %}
when I put this into a for loop, I get not the answer I'd like to.
the code itself:
{% for j in 0..6 %}
{% set datum = date(current_user.cwmon)|date_modify("+1 day")|date('D d.m.y') %}
// other code
{{ j }}: {{ datum }}
// other code
{% endfor %}
is there a way to use my var j instead of +1 day?
Whatever I try I get an error.
my desired result:
0: Mon 15.01.19
1: Tue 16.01.19
...
6: Sun 20.01.19
Thank you very much in advance.
apparently the answer is quite simple.
{% for j in 0..6 %}
{% set datum = YOUR_DATE|date_modify("+" ~ j ~ " day")|date('D d.m.y') %}
{% endfor %}
with this, datum has the correct value and adds j to itself.
Another solution is overwriting the datum variable
{% set datum = current_user.cwmon %}
{% for j in 0..6 %}
{% set datum = date(datum)|date_modify("+1 day")|date('D d.m.y') %}
// other code
{{ j }}: {{ datum }}
// other code
{% endfor %}
demo

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.

how to subtract dates in twig?

{%for mat in setQuery %}
{% set datePost = mat.data_criacao|date('d-m-Y') %}
{% set today = "now"|date('d-m-Y') %}
{{today- datePost}}
{% endfor %}
datePost = 17-04-2015
today = 06-05-2015
the example above returns it: -11
The issue was resolved with the following code:
{% set datePost = mat.data_criacao|date('d-m-Y') %}
{% set today = "now"|date('d-m-Y') %}
{% set difference = date(today).diff(date(datePost))%}
{% set leftDays = difference.days %}
{% if datePost == today %}
1 day
{% else %}
{{ leftDays }}
{% endif %}
You must write your custom twig extension:
You must write a twig function as described here with the following code for make diff via php function:
$calcFrom = $from;
$calcTo = $to;
$now->diff($calcFrom)->format("%a")
And make it available via a Twig extension.
If you are using symfony2 framework You can use the KnpTimeBundle
In the Twig:
This compare with the current date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(form) }}
This compare with the another date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(form , to ) }}
Hope this help

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