twig date_diff month not working properly - symfony

I try do date_diff last month, but have a problem if month have 31 day he say I'm to 01 at this month. And if do 2 month later give good result. But again every month who have 31 days give result to 01.
To be more clear i example you:
{{ "now" | date("Y-m-d") }} {# 2015-07-31 #}
{{ "now" | date_modify("-1 month") | date("Y-m-d") }} {# 2015-07-01 #}
{{ "now" | date_modify("-2 month") | date("Y-m-d") }} {# 2015-05-31 #}
{{ "now" | date_modify("-3 month") | date("Y-m-d") }} {# 2015-05-01 #}
Any1 have any ideea why ? Because any if modify month with even number you go at month start not to the end of previous month.

Based on the answer here it seems that when PHP modifies a DateTime() object with the string '-1 month' it simply decrements the month value, if I understand correctly.
So given your example, you start with today's date: 2015-07-31.
PHP changes this to 2015-06-31. However, there are only 30 days in June. So it increments this up to the next date that makes sense, which is... 2015-07-01.
I tried replicating this with:
echo (new DateTime())->sub(new DateInterval('P1M'))->format('Y-m-d');
and:
$dt = new DateTime();
$dt->modify('-1 month');
echo $dt->format('Y-m-d');
and I got the exact same result in each case:
2015-07-01
So I guess it's just one of PHP's foibles. Pretty messy, a lot can happen in a day!

Related

I want to find the day difference between 2 date column in azure app insight?

We have a log file where we store the searches happening on our platform. Now there is a departure date and I want to find the searches where departure date is after 330 days from today.
I am trying to run the query to find the difference between departure date column and logtime(entry time of the event into log). But getting the below error:
Query could not be parsed at 'datetime("departureDate")' on line [5,54]
Token: datetime("departureDate")
Line: 5
Position: 54
Date format of departure date is mm/dd/yyyy and logtime format is typical datetime format of app insight.
Query that I am running is below:
customEvents
| where name == "SearchLog"
| extend departureDate = tostring(customDimensions.departureDate)
| extend logTime = tostring(customDimensions.logTime)
| where datetime_diff('day',datetime("departureDate"),datetime("logTime")) > 200
As suggested I ran the below query but now I am getting 0 results but there is data that satisfy the given criteria.
customEvents
| where name == "SearchLog"
| extend departureDate = tostring(customDimensions.departureDate)
| extend logTime = tostring(customDimensions.logTime)
| where datetime_diff('day',todatetime(departureDate),todatetime(logTime)) > 200
Example:
departureDate
04/09/2020
logTime
8/13/2019 8:45:39 AM -04:00
I also tried the below query to check whether data format is supported or not and it gave correct response.
customEvents
| project datetime_diff('day', datetime('04/30/2020'),datetime('8/13/2019 8:25:51 AM -04:00'))
Please use the below query. Use todatetime statement to convert string to datetime
customEvents
| where name == "SearchLog"
| extend departureDate = tostring(customDimensions.departureDate)
| extend logTime = tostring(customDimensions.logTime)
| where datetime_diff('day',todatetime(departureDate),todatetime(logTime)) > 200
The double quotes inside datetime operator in where clause should be removed.
Your code should look like:
where datetime_diff('day',datetime(departureDate),datetime(logTime)) > 200

Reverse array in Twig

I'm doing a file explorer with Symfony 2.5 and PHP 5.3
In my Controller I return an array to my view (result of calling scandir(); on a directory)
The array contains "months". (January, February, March...) and I display it in a Bootstrap accordion.
I want to order by descending this array like:
December -> November -> October -> September ...
We can see that {{ for months in month|sort|reverse }} doesnt work here.
How can I do this please?
You need to sort array by index, otherwise it will sort array alphabetically:
{% set months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} %}
{% for month in months|reverse %}
{{ month }}
{% endfor %}
You can use the PHP function array_reverse(). It gives you the array in reverse order. So you don't have to do anything in Twig.
$input = array(1, 2, 3);
$reversed = array_reverse($input);
Of course you have to use this function in your controller.
To order months not alphabetically but by time, see this post.
PHP re-order array of month names
This question has already been answered. You just have to order them like this in your controller and you're done.

Twig: date format with localizeddate extension

I have:
<div class="date">
{{ event.date|localizeddate('full', 'none', null, 'Europe/Moscow', 'd MMMM YYYY, EEEE') }}
</div>
Where event.date = '2016-12-30', MySQL DATE string.
When date localized, output is '30 December 2017, Friday'. I don't understand, why year is changed to 2017? Why that happening?
Had the same issue in a recent project.
In the documentation it says Y stands for "year of "Week of Year""
and y is for the year.
http://userguide.icu-project.org/formatparse/datetime
try using this:
{{ event.date|date("m/d/Y")|localizeddate('full', 'none', null, 'Europe/Moscow', 'd MMMM YYYY, EEEE') }}

Round up a variable with Shopify Liquid

I wish to assign a dummy variable to a math value so I can then take the ceiling.
My current code is:
{% if variant.compare_at_price > variant.price %}
SAVE {{ variant.compare_at_price | minus:variant.price | times:100 | divided_by:variant.compare_at_price }}%
{% endif %}
Output is SAVE 20% (for example, but if it's 19.99 it'll be 19% rather than 20%)
But I want to call:
x= {{ variant.compare_at_price | minus:variant.price | times:100 | divided_by:variant.compare_at_price }}%
then take {{ x | ceil }}
How do I assign x?
By using the assign variable tag:
{% assign x = variant.compare_at_price | minus:variant.price | times:100.0 | divided_by:variant.compare_at_price %}
{% assign x = x | ceil %}
SAVE {{x}}%

Symfony - Twig - dateTime HH:MM:SS.MMM in SS.MMM?

How to convert a datetime HH:MM:SS:mmm in SS.mmm with a filter in Twig please ( `` )?
exemple :
00:01:30.600 => 90.600
I tried that {{ Object.time | date("s") }} but it doesn't work ...
thanks !
As #john Smith told in his comment (unfortunately I can't +1 but he deserves it), you can use:
{{ Object.time | date("s.u") }}
To change microseconds to miliseconds, you have 2 choices:
Round to 3 digits:
{{ Object.time | date("s.u") | round(3) }}
Slice the 3 last chars:
{{ Object.time | date("s.u") | slice(0, -3) }}

Resources