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

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

Related

Is there an configuration to limit the numbers of page numbers in Pagerfanta pagination?

I have a pagination with Pagerfanta. I want to limit page numbers to 5, since on mobile version pagination is too large. I am using default view 'twitter_bootstrap_translated'.
{% if articles.haveToPaginate %}
<div class="pagination-class">
{{ pagerfanta(articles, 'twitter_bootstrap_translated', {routeName: 'search_result_paginated', routeParams: app.request.query.all}) }}
</div>
{% endif %}
How I can limit the page numbers/links?
Currently: < Prev | 1 | 2 | 3 | 4 | 5 | 6 | 7 | ... 101 | Next >
I need pagination like this:
Currently: < Prev | 1 | 2 | 3 | ... 101 | Next >
Please have a look might be useful:
<?php
use Pagerfanta\View\TwitterBootstrapView;
$view = new TwitterBootstrapView();
$options = array('proximity' => 3);
$html = $view->render($pagerfanta, $routeGenerator, $options);
Options (default):
proximity (3)
prev_message (← Previous)
prev_disabled_href ()
next_message (Next →)
next_disabled_href ()
dots_message (…)
dots_href ()
css_container_class (pagination)
css_prev_class (prev)
css_next_class (next)
css_disabled_class (disabled)
css_dots_class (disabled)
css_active_class (active)

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}}%

twig date_diff month not working properly

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!

Render SQL result in twig file

How can i render result of following query in twig ?
$q = $em->createQuery('
SELECT mark, student.studentName FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
WHERE student.id = mark.studentId'
);
$marks = $q->getArrayResult();
Table contents are
Table tbl_mark_details || tbl_student_details
---------------------------------------------------------------
id | student_id | exam_id | score || id | student name
1 |1 | 1 |10 || 1 | Student 1
2 |2 | 1 |5 || 2 | Student 2
3 |2 | 2 |25 ||
I tried the following code, but no student name for the third row
{% set i=0 %}
{% for mark in marks %}
{% if i%2 == 0 %}
<tr>
<td>{{ mark.score }}</td>
<td>{{ mark.examId }}</td>
{% else %}
<td>{{ mark.studentName }}</td>
</tr>
{% endif %}
{% set i = i+1 %}
{% endfor %}
Thanks
Vishnu V
Your should try like this :
$q = $em->createQuery('
SELECT mark, student FROM DemoTemplateBundle:TblMarkDetails mark, DemoTemplateBundle:TblStudentDetails student
WHERE student.id = mark.studentId'
);
$marks = $q->getResult();
and your twig is all right ..

how make addition from 2 variable twig?

Anyone knows how to addition two variable in twig I want to do something like:
{{ var1 }} + {{ var2 }}
Just do it inside the {{ }}
{{ var1 + var2 }}
If you want to assign it to some other variable:
{% set foo = var1 + var2 %}
{{ foo }}
Declaration :
{% set counter = 0 %}
Doing the addition :
{% set counter = counter + 1 %}
For displaying in twig template :
{{ counter }}

Resources