flack / ranger Goto Github PK
View Code? Open in Web Editor NEWFormatter for date and time ranges with i18n support
Formatter for date and time ranges with i18n support
When passing a unix timestamp, the formatter tries to format it as a string and throw an error:
Uncaught Exception: Failed to parse time string (1671062400) at position 8 (0): Unexpected character in (...)vendor/openpsa/ranger/src/Ranger.php:243
The issue is that prepare_date() first checks if the input is a string (although, technically, an integer is also a string).
I solved it by replacing
if (is_string($input)) {
return new Datetime($input);
}
if (is_int($input)) {
$date = new Datetime;
$date->setTimestamp($input);
return $date;
}
with
if (is_numeric($input)) {
$date = new Datetime;
$date->setTimestamp($input);
return $date;
}
if (is_string($input)) {
return new Datetime($input);
}
I also replaced is_int() by is_numeric(), which is the recommended way to distinguish a number from a string, is_int() failing in some cases.
Would it be possible to support dates where the times are set to 00:00 to 23:59 - this seems to be a popular setting to display an all day event.
It would be nice to have the option to pass in a string to read "all day"
This is a great project! It fills almost perfectly a need that I have. However, in my use-case, I would like to be more generic about the date range, by dropping the day field. For example, I currently get back from ranger->format()
the text Jan 8 – Oct 26, 2020
, but it would be nice to just say Jan – Oct 2020
. (Or for the example dates Oct 13–26, 2020
, simply saying Oct 2020
.)
I could see potential use cases for dropping even the month:
2020
2015–2020
)2019–2020
)1951–2013
)Is this theoretically possible in Ranger right now? Monkeying around with its internals, I seemed to find problems with creating an IntlDateFormatter using only parts of the date.
This is an edge case, but if your environment uses UTC offsets rather than named timezones:
$time = new \DateTime('2019-01-01')
$time->setTimezone(new \DateTimeZone('-0400'));
and then pass this into Ranger, you'll get this error:
IntlException: datefmt_create: no such time zone: '-0400': U_ILLEGAL_ARGUMENT_ERROR
Luckily there's an easy fix: initialize IntlDateFormatter
with the DateTimeZone object, rather than the name (see "timezone" parameter on http://php.net/manual/en/intldateformatter.create.php)
PR incoming.
Hi! Thanks a lot for publishing this handy library! I was formatting some date range in my PHP 7.4 app like this:
return (new Ranger($this->getLocale()))
->setDateType(\IntlDateFormatter::LONG)
->format($this->startsAt, $this->endsAt);
It works perfectly for most languages that I tried:
(en) December 3 – 4, 2020
(fr) 3 – 4 décembre 2020
(de) 3. – 4. Dezember 2020
But in Spanish (locale es
) I get this (it even includes an extra white space at the end):
3 de – 4 de diciembre
It should be:
3 – 4 de diciembre de 2020
(also common -> 3 – 4 de diciembre, 2020)
Maybe I did something wrong, but I wanted to report this issue just in case. Thanks!
I've modified my test program slightly:
<?php
use OpenPsa\Ranger\Ranger;
require_once 'lib/autoload.inc';
error_reporting(-1);
ini_set('display_errors', '1');
foreach (['en_GB', 'zh_TW'] as $locale) {
$ranger = new Ranger($locale);
$ranger
->setDateType(IntlDateFormatter::MEDIUM)
->setTimeType(IntlDateFormatter::SHORT);
echo $ranger->format(1711738800, 1711828800);
echo "<br>\n";
}
and now get the following output:
29 Mar 2024, 19:00 – 30 Mar 2024, 20:00
2024年3月29日, 晚上<<<>>>7:00 – 2024年3月30日, 晚上<<<>>>8:00
The problem only seems to happen if the start and end dates are on different days.
Library looks very useful.
Was not sure what the recommended way is to install.
If I provide the same date with the same time, the time output is weirdly merged.
$ranger = new OpenPsa\Ranger\Ranger('de');
$ranger->setTimeType(IntlDateFormatter::SHORT);
$ranger->format('2022-05-04 20:00:00', '2022-05-04 20:00:00');
The output is 04.05.2022, 20:00 – 00
.
I would except the output to be 04.05.2022, 20:00
.
If I run the following test code:
<?php
use OpenPsa\Ranger\Ranger;
require_once 'lib/autoload.inc';
error_reporting(-1);
ini_set('display_errors', '1');
foreach (['en_GB', 'zh_TW'] as $locale) {
$ranger = new Ranger($locale);
$ranger
->setDateType(IntlDateFormatter::MEDIUM)
->setTimeType(IntlDateFormatter::SHORT);
echo $ranger->format(1711738800, 1711738800);
echo "<br>\n";
}
I get the output
29 Mar 2024, 19:00
Warning: Undefined array key 8 in C:\inetpub\wwwroot\mrbs-main\lib\OpenPsa\Ranger\Ranger.php on line 195 Warning: Trying to access array offset on null in C:\inetpub\wwwroot\mrbs-main\lib\OpenPsa\Ranger\Ranger.php on line 195 2024年3月日, B日, B::
I am running the latest version of Ranger downloaded from the Master branch today using PHP 8.3.3 on Windows.
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.