0

I am working on Symfony application and using Twig for layout.

I facing wrong output issue i google it but can't find the solution.

I have date & time 201801031400 I used this

{{ val.start_date|date("m/d/Y") }}

but get the wrong output 10/27/8364

When i used this {{ "now"|date("m/d/Y") }} it give me correct output

Thanks in Advance!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Zaheer Ahmad
  • 176
  • 2
  • 11

3 Answers3

1

You need to a function to Twig to achieve this, you can see how to register an extension here

TwigExtension

namespace My/Project/Twig/Extensions

class ProjectTwigExtension extends Twig_Extension {

    public function getFunctions() {
        return array(
            new Twig_SimpleFunction('convert_api_date', function($date) {
                return new DateTime($date);         
            }),
        );  
    }

    public function getName() {
        return 'ProjectTwigExtension'; //this is mandatory
    }        
}

twig

{{ convert_api_date('201801031400') | date('d/m/Y') }}
{{ convert_api_date('201801031400') | date('H:i') }}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
0

Above answer will perfectly alright i just my solution code too.

First Twig date filter not support the 201801031400. so for this you have to make your own extension.

How to create twig extension create twig extention

<?php

// src/AppBundle/Twig/AppExtension.php
namespace AppBundle\Twig;

class DateParserFilter extends \Twig_Extension
{
    public function getFilters ()
    {
        return array(
            new \Twig_SimpleFilter('parse_date', array($this, 'parseDate'))
        );
    }
    public function parseDate ($string, $formats)
    {
        if (is_string($formats))
        {
            $formats = array($formats);
        }
        foreach ($formats as $format)
        {
            $dateTime = \DateTime::createFromFormat($format, $string);
            if ($dateTime !== false)
            {
                return $dateTime;
            }
        }
        return $string;
    }
    public function getName ()
    {
        return "parse_date";
    }
} 

after this use your own extension filter first then use twig builtin date filter example show below :)

{{ val.start_date | parse_date(["YmdHi", "d/m/Y H:i"]) | date("M d, Y") }}

Its depend what you have in my case i have [2018][01][03][14][00] = [Y][m][d][H][i]

hope it will help you :)

source of this extension - link

Zaheer Ahmad
  • 176
  • 2
  • 11
-2

You have to divide your unix timestamp value by 1000.

I'm still not sure with what it's related, but this operation is used by authoritative online converters, that most likely faced the same problem. I came to this conclusion thanks to this answer Converting a UNIX Timestamp to Formatted Date String.

Below is the proof that your unix timestamp is correct:

1) from www.freeformatter.com

2) from www.epochconverter.com

The data is slightly different due to the time zone. It must be taken into account.

Write in the comments, why should I divide by 1000. I myself will be interested.

Vitaly Vesyolko
  • 558
  • 5
  • 22