Last active
December 28, 2019 15:58
-
-
Save julp/b30a53d76f1d3a5b4a56 to your computer and use it in GitHub Desktop.
ICU dates relatives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function number_spellout($number, $ordinal = FALSE, $feminine = FALSE, $plural = FALSE, $locale = NULL) { | |
# see: http://saxonica.com/html/documentation/extensibility/config-extend/localizing/ICU-numbering-dates/ICU-numbering.html | |
static $map = array( | |
0b000 => '%spellout-cardinal-masculine', | |
0b001 => '%spellout-cardinal-masculine', // no plural | |
0b010 => '%spellout-cardinal-feminine', | |
0b011 => '%spellout-cardinal-feminine', // no plural | |
0b100 => '%spellout-ordinal-masculine', | |
0b101 => '%spellout-ordinal-masculine-plural', | |
0b110 => '%spellout-ordinal-feminine', | |
0b111 => '%spellout-ordinal-feminine-plural', | |
); | |
$numfmt = new NumberFormatter($locale ?: Locale::getDefault(), NumberFormatter::SPELLOUT); | |
if ($rule = $map[($ordinal << 2) | ($feminine << 1) | ($plural << 0)]) { | |
$numfmt->setTextAttribute(NumberFormatter::DEFAULT_RULESET, $rule); | |
} | |
return $numfmt->format($number); | |
} | |
var_dump( | |
number_spellout(1, FALSE, FALSE, FALSE), // un | |
number_spellout(1, FALSE, FALSE, TRUE), // un (pas de pluriel) | |
number_spellout(1, FALSE, TRUE, FALSE), // une | |
number_spellout(1, FALSE, TRUE, TRUE), // une (pas de pluriel) | |
number_spellout(1, TRUE, FALSE, FALSE), // premier | |
number_spellout(1, TRUE, FALSE, TRUE), // premiers # obtenu: un | |
number_spellout(1, TRUE, TRUE, FALSE), // première | |
number_spellout(1, TRUE, TRUE, TRUE), // premières # obtenu: un | |
NULL | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('Europe/Paris'); | |
if (!defined('IntlDateFormatter::RELATIVE')) { | |
define('RELATIVE', 1 << 7); | |
define('SHORT_RELATIVE', IntlDateFormatter::SHORT | RELATIVE); | |
define('FULL_RELATIVE', IntlDateFormatter::FULL | RELATIVE); | |
define('LONG_RELATIVE', IntlDateFormatter::LONG | RELATIVE); | |
define('MEDIUM_RELATIVE', IntlDateFormatter::MEDIUM | RELATIVE); | |
} | |
$datefmt = new IntlDateFormatter('fr_FR', FULL_RELATIVE, IntlDateFormatter::NONE); | |
var_dump( | |
INTL_ICU_VERSION, | |
$datefmt->format(new DateTime('yesterday')) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment