10
  • 如何获得与当天的天数差异。

  • 发推文在:“2015-02-22 09:56:42”。

枝条

{% for key,value in data.about %}
{% set tweets_date=(value.tweetedAt|date).date("now").format('%a') %}
{% endfor %}

我也试过

{% set dd='now'|date('d-m-Y') %}
{% set tweets_date=(value.tweetedAt|date).dd.format('%a') %}

最后我尝试了,但它给出了错误:

哈希键必须是带引号的字符串、数字、名称或括在括号中的表达式(值“{”的意外标记“标点符号”

{% set difference = {{ date("m/d/Y") }}.diff(date(value.tweetedAt)) %}
{% set leftDays = difference.days %}

错误:

<span class="small light_grey">{{tweets_date}}</span>

Impossible to invoke a method ("date") on a string variable ("June 6, 2015 01:06") in AcmeBundle
  • 如何从tweet_date几天前的形式中获得差异。

更新了我twig/extensions通过作曲家安装的项目

user@intermsh-OptiPlex-380:~$ composer require twig/extensions
Warning: This development build of composer is over 30 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
Using version ~1.2 for twig/extensions
./composer.json has been updated
   Loading composer repositories with package information
   Updating dependencies (including require-dev)
   - Installing twig/twig (v1.18.2)
   Downloading: 100%         

   - Installing twig/extensions (v1.2.0)
   Downloading: 100%         
   Downloading: 100%         

twig/extensions suggests installing symfony/translation (Allow the time_diff output to be translated)
Writing lock file
Generating autoload files
4

3 回答 3

13

我试过这段代码,它可以工作。

{% set difference = date(value.tweetedAt|date('Y/m/d')).diff(date('now'|date('Y/m/d'))) %}
{% set leftDays = difference.days %}
于 2015-09-03T01:54:06.943 回答
9

您需要在对象上使用过滤器而不是方法。你需要使用time_diff过滤器:

{%set tweets_date=value.tweetedAt|time_diff %}

要使其工作,您需要首先通过 composer 安装 twig-extensions:

composer require twig/extensions

然后将其包含在带有适当标签的服务容器中:

services:
    app.twig.extension.date:
        class:        Twig_Extensions_Extension_Date
        tags:
             - { name: 'twig.extension' }
于 2015-06-15T07:38:44.630 回答
3

如果日期来自实体,您可以:

{{ entity.days }}

并在您的实体中实现:

public function getDays()
{
    return $this->date->diff(new DateTime)->format('%a');
}
于 2016-09-02T00:18:16.017 回答