knplabs / knptimebundle Goto Github PK
View Code? Open in Web Editor NEWProvides helpers for time manipulation
Home Page: http://knplabs.com
License: MIT License
Provides helpers for time manipulation
Home Page: http://knplabs.com
License: MIT License
I use the ago function in the mailer twig template.
Before I change the local in the request to the user language. Unfortunately the translator of the ago function does not notice this and renders it in the original language.
Issue #103 removed a test that would prove this (failing) test with an (almost) identical DateTime (compared to time()
) for the |ago
filter:
public function testFormatDiffDefaultToIsNull()
{
$from = new \DatetimeImmutable(date('Y-m-d H:i:s', time()));
$to = new \DateTime(null);
$this->assertEquals('diff.empty', $this->formatter->formatDiff($from, $to));
// actually returns 'diff.in.year' - or 'in -1 years' for English translation
}
In a live code scenario it would happen like this in a twig template (here, profile->updatedAt is a simple DateTime, with seconds resolution):
{# profile.updatedAt is a simple DateTime #}
<p>Updated: {{ profile.updatedAt|ago() }}</p>
But, the ago()
uses the profile.updatedAt
as the first parameter. and defaults to NULL as the second, from the twig filter TimeExtension::diff() (calling TimeHelper::diff()).
TimeHelper::diff()
, via getDatetimeObject()
, takes the null and makes it a DateTime(), defaulting to 'now' (with microseconds since PHP7.2):
return new DateTime(null);
Hence, the test above, which if you edit a profile and immediately show the updatedAt|ago()
in a twig template can show 'in -1 Year' as the result.
If the default, in getDatetimeObject()
was return new DateTime(null ?? '@'.time());
solves the issue, but setting it to DateTime('now')
would still fail because of the tiny difference in microseconds.
Many users of bundle will want to see '2.59 minutes' for 179 seconds instead of '2 minutes' :)
Cheers !
It's show only diff.ago.hour. translation is not included by default
translator: { fallbacks: ["%locale%"] } is activated
Symfony 3.1
Hello @weaverryan !
I hope you are doing fine ! 🌻
We are trying to uniform documentation on our public repositories adding code of conducts and contributing guidelines (you can check Snappy repository as an example CODE_OF_CONDUCT.md and CONTRIBUTING.md).
Do you mind if we create a PR to add them on this repository as well (the contributing guidelines will be more generic than Snappy's ones)?
Have a nice day !
Eve
Could you please upgrade composer.json so that requirements met new Symfony 2.2?
"symfony/framework-bundle": ">=2.0,<2.3-dev"
Otherwise we cannot use this package on latest dev versions, and composer throws conflicts. Right now this package is limited to less than 2.2:
"symfony/framework-bundle": ">=2.0,<2.2-dev"
Thank you
Hello,
Do you think it would be nice to add a feature to get the age?
For example :
{# with filter: #}
Age: {{ user.birthdate|age }} <!-- Age: 30 years old -->
{# ... or use the equivalent function: #}
Age: {{ age(user.birthdate) }} <!-- Age: 30 years old -->
Good Day,
How can I translate my 'ago' output in another language than English?
I'm an entry-level user/
Rgds
I am attempting to make use of the Twig ago
filter in my project. However, for various reasons that are not relevant here, I have the translator component disabled. As a result, this filter (and the bundle as a whole) does not work.
I understand why this bundle uses the translator component, but that utilization should be a feature, not a prerequisite.
Any chance something will be done to change this?
Add the interface so we can inject a different formatter. We want to use differencing without having to turn on translation. Currently you inject a concrete instance of DateTimeFormatter
into TimeHelper
using the container.
I know that the current minimum required PHP version for this bundle is 5.3.3, but adding DateTimeImmutable support (only available since PHP 5.5) would be really great - I would even be glad to make the necesarry changes myself.
I'm trying to get the ago date from my controller.php , can you explain with an exmple how do we use the helper :
if we have $date1 represent the date how do we call heplers and then call the diff function ?
tried use Knp\Bundle\TimeBundle\Templating\Helper as Helper;
$h=new Helper('Y-m-d H:i:s');
but this didnt work for me
Meta issue to track formatted duration (#177) translations.
Required translation keys:
KnpTimeBundle/translations/time.en.xliff
Lines 57 to 76 in 3fe7960
On latest version of twig bundle an error occurred:
Attempted to load class "Twig_Extension" from the global namespace.
!! Did you forget a "use" statement?
Using the "Twig_Extension" class is deprecated since Twig version 2.7
You can check it if install Symfony 5 and then try to install KnpTimeBundle
composer require knplabs/knp-time-bundle outputs error:
- knplabs/knp-time-bundle v1.2 requires symfony/framework-bundle >=2.0,<2.4-dev -> satisfiable by symfony/framework-bundle[2.0.x-dev, 2.1.x-dev, 2.2.x-dev, 2.3.x-dev].
Adding support for Syfmony4 would be nice!
Pull requests waiting for merge, maybe addressing this issue?
#103
When adding "symfony/symfony": "~2.4",
to my composer.json I get Your requirements could not be resolved to an installable set of packages.
Removing "knplabs/knp-time-bundle": "~1.2",
from my composer.json file makes things install ok.
Here is the full error:
Problem 1
- symfony/framework-bundle 2.0.7 requires symfony/translator 2.0.7 -> no matching package found.
- Conclusion: don't install symfony/symfony 2.5.x-dev
- Conclusion: don't install symfony/symfony 2.4.x-dev
- Conclusion: don't install symfony/symfony v2.4.0-RC1
- app 2.3.x-dev requires knplabs/knp-time-bundle ~1.2 -> satisfiable by knplabs/knp-time-bundle[v1.2].
- knplabs/knp-time-bundle v1.2 requires symfony/framework-bundle >=2.0,<2.4-dev -> satisfiable by symfony/symfony[2.3.x-dev, 2.0.7, 2.0.x-dev, 2.1.x-dev, 2.2.x-dev, v2.0.10, v2.0.11, v2.0.12, v2.0.13, v2.0.14, v2.0.15, v2.0.16, v2.0.17, v2.0.18, v2.0.19, v2.0.20, v2.0.21, v2.0.22, v2.0.23, v2.0.24, v2.0.25, v2.0.9, v2.1.0, v2.1.1, v2.1.10, v2.1.11, v2.1.12, v2.1.13, v2.1.2, v2.1.3, v2.1.4, v2.1.5, v2.1.6, v2.1.7, v2.1.8, v2.1.9, v2.2.0, v2.2.1, v2.2.10, v2.2.2, v2.2.3, v2.2.4, v2.2.5, v2.2.6, v2.2.7, v2.2.8, v2.2.9, v2.3.0, v2.3.1, v2.3.2, v2.3.3, v2.3.4, v2.3.5, v2.3.6, v2.3.7], symfony/framework-bundle[2.0.7, 2.0.x-dev, 2.1.x-dev, 2.2.x-dev, 2.3.x-dev, v2.0.10, v2.0.12, v2.0.13, v2.0.14, v2.0.15, v2.0.16, v2.0.17, v2.0.18, v2.0.19, v2.0.20, v2.0.21, v2.0.22, v2.0.23, v2.0.24, v2.0.25, v2.0.9, v2.1.0, v2.1.1, v2.1.10, v2.1.11, v2.1.12, v2.1.13, v2.1.2, v2.1.3, v2.1.4, v2.1.5, v2.1.6, v2.1.7, v2.1.8, v2.1.9, v2.2.0, v2.2.1, v2.2.10, v2.2.2, v2.2.3, v2.2.4, v2.2.5, v2.2.6, v2.2.7, v2.2.8, v2.2.9, v2.3.0, v2.3.1, v2.3.2, v2.3.3, v2.3.4, v2.3.5, v2.3.6, v2.3.7].
$ composer require knplabs/knp-time-bundle
Using version ^1.7 for knplabs/knp-time-bundle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache
[Symfony\Component\Filesystem\Exception\IOException]
Failed to remove directory "C:\xampp\htdocs\coi\var\cache\de~\profiler\c0":
.
cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--]
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the symfony-scripts event terminated with an exception
Installation failed, reverting ./composer.json to its original content.
[RuntimeException]
An error occurred when executing the ""cache:clear --no-warmup"" command:
[Symfony\Component\Filesystem\Exception\IOException]
Failed to remove directory "C:\xampp\htdocs\coi\var\cache\de~\profiler\c0
":
.
cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet]
[-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interact
ion] [-e|--env ENV] [--no-debug] [--]
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] []...
Hello!
I've just updated to 2.2 since 1.20 and i noticed that i cant call the filters with the apply_filter
Results in this error:
An exception has been thrown during the rendering of a template ("call_user_func(): Argument #1 ($callback) must be a valid callback, non-static method Knp\Bundle\TimeBundle\DateTimeFormatter::formatDiff() cannot be called statically").
Steps to reproduce:
// test.html.twig
{% set date = date() %} // or any date from controller, db, etc
{{ date|ago }} //works
{{ date|time_diff }} //works
{{date|apply_filter('time_diff')}} //doesn't work
{{date|apply_filter('ago')}} //doesn't work
Im required to apply_filter because 'ago' is being set dynamically. Sure, i can just put an if statement and call the filter directly, but im guessing this is a compatibility error.
Any thoughts? Thanks!
Installation of the latest master branch results in the following error (Twig 3 & SF 5):
% composer require knplabs/knp-time-bundle dev-master
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Restricting packages listed in "symfony/symfony" to "5.0.*"
Package operations: 2 installs, 0 updates, 0 removals
- Installing symfony/templating (v5.0.2): Loading from cache
- Installing knplabs/knp-time-bundle (dev-master ab88c33): Downloading (100%)
Writing lock file
Generating autoload files
ocramius/package-versions: Generating version class...
ocramius/package-versions: ...done generating version class
Symfony operations: 1 recipe (4f0778d90f6e5ea704fa8d15747dd47c)
- Configuring knplabs/knp-time-bundle (>=dev-master): From auto-generated recipe
Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 255
!!
!! // Clearing the cache for the dev environment with debug
!! // true
!!
!! 16:01:05 CRITICAL [php] Uncaught Error: Argument 1 passed to Knp\Bundle\TimeBundle\DateTimeFormatter::__construct() must be an instance of Symfony\Component\Translation\TranslatorInterface, instance of Symfony\Component\Translation\DataCollectorTranslator given, called in /Users/USER/Sites/apps/PROJECT/var/cache/dev/ContainerXX8gsrP/App_KernelDevDebugContainer.php on line 1773 ["exception" => TypeError { …}]
!!
!! In DateTimeFormatter.php line 17:
!!
!! Argument 1 passed to Knp\Bundle\TimeBundle\DateTimeFormatter::__construct()
!! must be an instance of Symfony\Component\Translation\TranslatorInterface,
!! instance of Symfony\Component\Translation\DataCollectorTranslator given, ca
!! lled in /Users/USER/Sites/apps/PROJECT/var/cache/dev/
!! ContainerXX8gsrP/App_KernelDevDebugContainer.php on line 1773
!!
!!
!! cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
!!
!!
Script @auto-scripts was called via post-update-cmd
Installation failed, reverting ./composer.json to its original content.
Solution (DateTimeFormatter.php):
Line 5 old:
use Symfony\Component\Translation\TranslatorInterface;
Line 5 new:
use Symfony\Component\Translation\DataCollectorTranslator;
Line 17 old:
public function __construct(TranslatorInterface $translator)
Line 17 new:
public function __construct(DataCollectorTranslator $translator)
Hey guys,
Do you think it would be possible to have the ability to wrap the time diff in an <abbr>
tag?
So, something like
We shipped your order {{ order.shippedAt|ago }}.
would produce:
We shipped your order <abbr title="2015-03-04 10:00:00">2 months ago</abbr>
Not sure if this would be controlled by config or if there'd be a separate filter (ago_abbr
).
Let me know what you think.
Cheers,
Tom
Is this a problem of KnpTimeBundle?
[2012-06-30 12:56:00] request.CRITICAL: Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Unable to choose a translation fo
r "wczoraj|%count% dni temu" with locale "pl".") in "KnpBundlesBundle::layout.html.twig" at line 55. (uncaught exception) at app/cache/prod/classes.php line 8510 [] []
could you please check?
Hi,
I have look on how to solve this problem for a bit now and believe that it is not possible with the current code.
I need to send a batch of emails to different users with different locales. In these emails, I want to use the ago function. It seems that since I use it in the console environment, the translator it set on the default_locale witch does not match the locale of some users.
Thank you!
Hello KnpLabs, I am happily using the current version of your bundle.
If I use the ago function today ("09/16/2023") for the date "02/27/1900", the result is: 3 years ago.
Is it possible to display "older" data correctly?
Packagist currently references this commit: ad2aecd
Which right now does not appear in the repository so my guess is that it has been removed after the push. However Packagist has not taken this into account. Which is why it's impossible to install via composer right now.
That commit says that KNPLabs does not want to keep maintaining this bundle but has been removed. If the end of support is actually the case, I could be interested in taking over this bundle if this solution is ok for you as well. Let me know.
I've tried to do a pull request, but I always get permission denied (it's my very first time contributing, so maybe I am doing something wrong), so I open this issue to share with all of you the catalan translations for this bundle:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="ca" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>diff.ago.year</source>
<target>fa 1 any|fa %count% anys</target>
</trans-unit>
<trans-unit id="2">
<source>diff.ago.month</source>
<target>fa 1 mes|fa %count% mesos</target>
</trans-unit>
<trans-unit id="3">
<source>diff.ago.day</source>
<target>fa 1 dia|fa %count% dies</target>
</trans-unit>
<trans-unit id="4">
<source>diff.ago.hour</source>
<target>fa 1 hora|fa %count% hores</target>
</trans-unit>
<trans-unit id="5">
<source>diff.ago.minute</source>
<target>fa 1 minut|fa %count% minuts</target>
</trans-unit>
<trans-unit id="6">
<source>diff.ago.second</source>
<target>fa 1 segon|fa %count% segons</target>
</trans-unit>
<trans-unit id="7">
<source>diff.empty</source>
<target>ara</target>
</trans-unit>
<trans-unit id="8">
<source>diff.in.second</source>
<target>d'aquí 1 segon|d'aquí %count% segons</target>
</trans-unit>
<trans-unit id="9">
<source>diff.in.hour</source>
<target>d'aquí 1 hora|d'aquí %count% hores</target>
</trans-unit>
<trans-unit id="10">
<source>diff.in.minute</source>
<target>d'aquí 1 minut|d'aquí %count% minuts</target>
</trans-unit>
<trans-unit id="11">
<source>diff.in.day</source>
<target>d'aquí 1 dia|d'aquí %count% dies</target>
</trans-unit>
<trans-unit id="12">
<source>diff.in.month</source>
<target>d'aquí 1 mes|d'aquí %count% mesos</target>
</trans-unit>
<trans-unit id="13">
<source>diff.in.year</source>
<target>d'aquí 1 any|d'aquí %count% anys</target>
</trans-unit>
</body>
</file>
</xliff>
After Symfony
upgrade to v6.4
got this deprecation notice:
The "Knp\Bundle\TimeBundle\Templating\Helper\TimeHelper" class extends "Symfony\Component\Templating\Helper\Helper" that is deprecated since Symfony 6.4, use Twig instead.
An exception has been thrown during the rendering of a template ("Unable to choose a translation for "diff.ago.day" with locale "es"
to open the page I get this error, check if the translations are well written and if the format is well
en
<trans-unit id="3">
<source>diff.ago.day</source>
<target>1 day ago|%count% days ago</target>
</trans-unit>
es
<trans-unit id="3">
<source>diff.ago.day</source>
<target>hace 1 día|hace %count% días</target>
</trans-unit>
Ability to output something similar to:
"in 5-10 days"
templates
{{ time_diff('2012-10-05 16:47:13') }}
result 'diff.ago.minute'
and how to change the language? in MyProject/app/Resources/translations copy
Are there any plans for adding compatibilty with PHP 8.0?
Would you like me to create a pull request for this?
I see that "symfony/translation": "~3.4
dependency was removed in the f3d0151 commit.
It looks like it was unintentional given adjacent dependencies still support 3.4 branch of the symfony.
Would it be possible to have it back?
Hi,
Maybe I'm doing something wrong, but I can't tell from the description. I add the time extension to Twig, but then I get an Uncaught Twig\Error\RuntimeError error.
$twig->addExtension(new \Knp\Bundle\TimeBundle\Twig\Extension\TimeExtension());
Uncaught Twig\Error\RuntimeError: Unable to load the "Knp\Bundle\TimeBundle\DateTimeFormatter" runtime in "_string_template
I know is not difficult, but I thing that nowadays every is using or going to use composer to deal with symfony2.x
So just for having this bundle perfect ;)
I note that there is a branch for 2.2 support. Could this be merged in to master? Also tagging a 1.0 release might be nice too.
@docteurklein could we get a new release?
Symfony5 is out, not sure what's needed to update this bundle. My attempt to simply bump the dependencies didn't work, can we drop PHP5 support?
Also, I think symfony/templating has been deprecated in favor of twig/twig, but I'm not sure how to provide BC for this.
It would be great if we could change precision of displayed diff. For example, currently date("-34 days")|ago
shows month ago
. But maybe it could be some parameter to ago
to make this more precise, showing 34 days ago
, without rounding it?
We would need the translations for the formatted age (#192)
Required translation keys:
KnpTimeBundle/translations/time.fr.xliff
Lines 77 to 80 in e574cf4
in 2 hours
Or something like that, for dates in future
Hello devs! Someone ever come across this issue?
I am having diff.ago.day
or diff.ago.time
displayed each time.
I have version "knplabs/knp-time-bundle": "^2.2"
running on Symfony 6.3
I have removed and reinstall, dump translations and cleared my cache and everything I can think of.
Thanks for the help.
I just used this bundle, and it's very useful.
But what I would like to do, is configure it to return hours and minutes differences only, and if the time diff exceeds a day, then just return a localizeddate.
I hope I wrote this properly
Hello,
in my translation files there is following mistake:
<trans-unit id="1">
<source>diff.ago.year</source>
<target>vor einem Jahr|vor %count% Jahren</target>
</trans-unit>
So I get results like this: vor einer Minute|vor 56 Minuten (one minute ago|56 minutes ago)
This bug is in all translations files and for all translations.
symfony/symfony-templating is set as dev requirement, but the TimeHelper class is depending on it
Hi,
getDiffMessage
is not used for this bundle :
https://github.com/KnpLabs/KnpTimeBundle/blob/master/DateTimeFormatter.php#L52
Hi!
Can you correct your translation in Polish version please? Instead of za dzień
, use jutro
.
The installation just crashes....
- Installing knplabs/knp-time-bundle (dev-master)
Cloning ad2aecda46601eeeaa426dec49938de527e363bb
[RuntimeException]
Failed to clone http://github.com/KnpLabs/KnpTimeBundle via git, https and http protocols, aborting.
- git://github.com/KnpLabs/KnpTimeBundle
fatal: reference is not a tree: ad2aecda46601eeeaa426dec49938de527e363bb
- https://github.com/KnpLabs/KnpTimeBundle
fatal: reference is not a tree: ad2aecda46601eeeaa426dec49938de527e363bb
- http://github.com/KnpLabs/KnpTimeBundle
fatal: reference is not a tree: ad2aecda46601eeeaa426dec49938de527e363bb
Symfony does not have the xml
translation loader by default, which causes the
The "xml" translation loader is not registered.
exception.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.