如何减去枝条中的日期?

时间:2015-05-06 15:00:31

标签: twig

public class FMResultSet
{
    private boolean columnNamesSetup;
    private HashMap<String, Number> columnNameToIndexMap;
    private Cursor rawQuery;

    public FMResultSet(Cursor rawQuery)
    {
        this.rawQuery = rawQuery;
    }

    public void close()
    {
        rawQuery.close();
    }

    public void setupColumnNames()
    {

        if (columnNameToIndexMap == null)
        {
            this.setColumnNameToIndexMap(new HashMap());
        }

        int columnCount = rawQuery.getColumnCount();

        int columnIdx = 0;
        for (columnIdx = 0; columnIdx < columnCount; columnIdx++)
        {
            columnNameToIndexMap.put(rawQuery.getColumnName(columnIdx).toLowerCase(), new Integer(columnIdx));
        }
        columnNamesSetup = true;
    }

    public boolean next()
    {
        return rawQuery.moveToNext();
    }

    public int columnIndexForName(String columnName)
    {

        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        columnName = columnName.toLowerCase();

        Number n = columnNameToIndexMap.get(columnName);

        if (n != null)
        {
            return NumberValueUtil.intVal(n);
        }

        Log.i("StdLog", String.format("Warning: I could not find the column named '%s'.", columnName));

        return -1;
    }

    public int intForColumn(String columnName)
    {

        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        int columnIdx = this.columnIndexForName(columnName);

        if (columnIdx == -1)
        {
            return 0;
        }

        return intForColumnIndex(columnIdx);
    }

    public int intForColumnIndex(int columnIdx)
    {
        return rawQuery.getInt(columnIdx);
    }

    public long longForColumn(String columnName)
    {

        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        int columnIdx = this.columnIndexForName(columnName);

        if (columnIdx == -1)
        {
            return 0;
        }

        return longForColumnIndex(columnIdx);
    }

    public long longForColumnIndex(int columnIdx)
    {
        return (long) rawQuery.getLong(columnIdx);
    }

    public boolean boolForColumn(String columnName)
    {
        return (this.intForColumn(columnName) != 0);
    }

    public boolean boolForColumnIndex(int columnIdx)
    {
        return (this.intForColumnIndex(columnIdx) != 0);
    }

    public double doubleForColumn(String columnName)
    {

        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        int columnIdx = this.columnIndexForName(columnName);

        if (columnIdx == -1)
        {
            return 0;
        }

        return doubleForColumnIndex(columnIdx);
    }

    public double doubleForColumnIndex(int columnIdx)
    {
        return rawQuery.getDouble(columnIdx);
    }

    public String stringForColumnIndex(int columnIdx)
    {
        return rawQuery.getString(columnIdx);
    }

    public String stringForColumn(String columnName)
    {

        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        int columnIdx = this.columnIndexForName(columnName);

        if (columnIdx == -1)
        {
            return null;
        }

        return this.stringForColumnIndex(columnIdx);
    }

    public Date dateForColumn(String columnName)
    {

        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        int columnIdx = this.columnIndexForName(columnName);

        if (columnIdx == -1)
        {
            return null;
        }

        return new Date((this.longForColumn(columnName)));
    }

    public Date dateForColumnIndex(int columnIdx)
    {
        return new Date((this.longForColumnIndex(columnIdx)));
    }

    public byte[] dataForColumn(String columnName)
    {
        if (!columnNamesSetup)
        {
            this.setupColumnNames();
        }

        int columnIdx = this.columnIndexForName(columnName);

        if (columnIdx == -1)
        {
            return null;
        }

        return this.dataForColumnIndex(columnIdx);
    }

    public byte[] dataForColumnIndex(int columnIdx)
    {

        return rawQuery.getBlob(columnIdx);
    }

    public HashMap columnNameToIndexMap()
    {
        return columnNameToIndexMap;
    }

    public void setColumnNameToIndexMap(HashMap value)
    {

        columnNameToIndexMap = value;
    }

    @SuppressLint("NewApi")
    public int getType(String string)
    {
        return rawQuery.getType(columnIndexForName(string));
    }

}

datePost = 17-04-2015 今天= 06-05-2015

上面的例子返回它:-11

2 个答案:

答案 0 :(得分:2)

您必须编写自定义树枝扩展名

您必须使用以下代码编写一个twig函数,如here所述,通过php函数进行make diff:

$calcFrom = $from;

$calcTo = $to;
$now->diff($calcFrom)->format("%a")

并通过Twig extension提供。

如果您使用的是symfony2框架您可以使用KnpTimeBundle

在树枝上: 这与当前日期进行比较:

{# Returns something like "3 minutes ago" #}
{{ time_diff(form) }}

这与另一个日期比较:

{# Returns something like "3 minutes ago" #}
{{ time_diff(form , to ) }}

希望这个帮助

答案 1 :(得分:2)

问题已解决

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