How to render a DateTime object in a Twig template
Although you can use the
{{ game.gameDate|date('Y-m-d') }}
approach, keep in mind that this version does not honor the user locale, which should not be a problem with a site used by only users of one nationality. International users should display the game date totally different, like extending the \DateTime
class, and adding a __toString()
method to it that checks the locale and acts accordingly.
Edit:
As pointed out by @Nic in a comment, if you use the Intl extension of Twig, you will have a localizeddate
filter available, which shows the date in the user’s locale. This way you can drop my previous idea of extending \DateTime
.
Symfony Twig (3) renders wrong datetime when datetime in database is 0000-00-00 00:00:00
Dates and times stated on our calendar get messy before the invention of the Gregorian calendar and clocks/timezones were accurately synced. The year 1 never existed at the time, and was invented well after the fact. Also note that the timezone offset is +0:53 minutes.
My guess is that this has something to do with 0000-00-00 00:00:00
being converted to a unix timestamp, and then re-formatted as a date-time again. Given that 0000-00-00 00:00:00
never existed, the algorithms to do these conversions might not be entirely the same or reversible MySQL and PHP.
My suggestion would be to not do this. If you need a value to indicate 'no time', use NULL
.
How can I correctly print a date in a twig template?
The date filter accepts strings (it must be in a format supported by the strtotime function), DateTime instances,
enter link description here
You can create a twig extension
<?php
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
use CG\Core\ClassUtils;
class PostExtension extends \Twig_Extension{
protected $loader;
protected $controller;
public function __construct(FilesystemLoader $loader)
{
$this->loader = $loader;
}
public function setController($controller)
{
$this->controller = $controller;
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
'dateFormater' => new \Twig_Function_Method($this, 'dateFormater', array('is_safe' => array('html'))),
);
}
public function dateFormater($dateTime){
$now = new \DateTime('NOW');
return $now->format( 'd-m-Y' ); // any other format !!
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'some_extension';
}
}
now add it as a service !
<services>
<service id="twig.extension.postExtension" class="Project\PostBundle\Twig\Extension\PostExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="twig.loader" />
</service>
<service id="project.post.listener" class="Project\PostBundle\EventListener\ControllerListener">
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" />
<argument type="service" id="twig.extension.postExtension" />
</service>
</services>
so finally you can use it as a filter in your twig code
{{ dateFormater(entity.date) }}
enjoy !
Twig - format date
try this
{{ entry.dateUpdated | date('j. F Y') }}
How can I make a custom time format using TWIG?
Everyone who read this question and answered or commented didn't actually understand the question. The OP wants to print 'h' or 'u' between the hours and minutes.
You can do that by using double backslash in front of any character you want to escape. So like so for your example:
{{ created_date|date('H\\hi') }}
or
{{ created_date|date('H\\ui') }}
Here is a twigfiddle example:
https://twigfiddle.com/ypnau1
Twig: how to check if a variable is a DateTime object
You should create a twig function
AcmeBundle\Twig\CheckExtension.php
<?php
namespace AcmeBundle\Twig;
class CheckExtension extends \Twig_Extension
{
public function getFunctions() {
return array(
'isDateTime' => new \Twig_Function_Method($this, 'isDateTime'),
);
}
public function isDateTime($date) {
return ($date instanceof \DateTime); /* edit */
}
public function getName()
{
return 'acme_check_extension';
}
}
services.yml
services:
acme_check_extension:
class: AcmeBundle\Twig\CheckExtension
tags:
- { name: twig.extension }
in your template:
{% if isDateTime(post.date) %}
...
{% endif %}
Convert DateTime Object to Date input Date on Twig
Just had modify a little bit.
<input name="birth" class="form-control" type="date" value="{{userProfile.birthDate is null ? "" : userProfile.birthDate.format('Y-m-d')|date('Y-m-d') }}">
Related Topics
How to Install/Enable the PHP Phar Extension
Call a C Program from PHP and Read Program Output
PHP Configuration: It Is Not Safe to Rely on the System's Timezone Settings
Executing a Shell Script in Background with PHP
Why Don't PHP Attributes Allow Functions
Generating Random Numbers Without Repeats
Naming Convention Issues When Using Codeigniter in Windows and Linux
Why We Need Curlopt_Ssl_Verifypeer in Windows
How to Check If There Is a There Is a Wget Instance Running
PHP Failed to Open Stream: Is a Directory
Google_Service_Directory - (403) Not Authorized to Access This Resource/Api
Is Include()/Require() with "Side Effects" a Bad Practice
How to Replace (Monkeypatch) PHP Functions