Home >Backend Development >PHP Tutorial >Localizing Dates, Currency, and Numbers with Php-Intl
This tutorial builds upon the previous introduction to the PHP Intl extension, focusing on localizing complex data like numbers, dates, and currencies. Let's dive in!
Key Concepts:
NumberFormatter
handles number localization, addressing variations in decimal separators and formatting styles across different locales.NumberFormatter
, specifying the currency code and using the formatCurrency
method.IntlCalendar
) for date manipulation and comparison, offering functionalities similar to popular date/time libraries.Decimal Localization:
Inconsistencies in decimal separators across regions are a common challenge. The NumberFormatter
class elegantly solves this:
<code class="language-php">$numberFormatter = new NumberFormatter( 'de_DE', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); // Output: string(11) "123.456.789" $numberFormatter = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); // Output: string(11) "123,456,789" $numberFormatter = new NumberFormatter( 'ar', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); // Output: string(22) "١٢٣٬٤٥٦٬٧٨٩" $numberFormatter = new NumberFormatter( 'bn', NumberFormatter::DECIMAL ); var_dump( $numberFormatter->format(123456789) ); // Output: string(30) "১২,৩৪,৫৬,৭৮৯"</code>
The locale code (e.g., 'de_DE', 'en_US') dictates the formatting style. Various formatting styles (decimal, currency, duration, etc.) are available.
Formatting Styles and Attributes:
We can customize number formatting using attributes:
<code class="language-php">$nf = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); $nf->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); var_dump( $nf->format(1234.56789) ); // Output: string(8) "1,234.57" var_dump( $nf->format(1234) ); // Output: string(8) "1,234.00"</code>
Rounding behavior can be controlled:
<code class="language-php">$nf = new NumberFormatter( 'en_US', NumberFormatter::DECIMAL ); $nf->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 2); $nf->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_CEILING); var_dump($nf->format(1234.5678) ); // Output: string(8) "1,234.57" $nf->setAttribute(NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_DOWN); var_dump($nf->format(1234.5678) ); // Output: string(8) "1,234.56"</code>
SPELLOUT
and DURATION
styles, introduced previously, also apply here. Parsing formatted strings back into numbers is supported via the parse
method.
Currency Localization:
Formatting numbers as currencies is straightforward:
<code class="language-php">$nf = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY ); var_dump( $nf->formatCurrency(1234.56789, "USD" ) ); // Output: string(9) ",234.57"</code>
The getSymbol
method simplifies currency symbol retrieval:
<code class="language-php">$nf = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY ); var_dump( $nf->formatCurrency(1234.56789, $nf->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL)) ); // Output: string(9) ",234.57" $nf = new NumberFormatter( 'fr_FR', NumberFormatter::CURRENCY ); var_dump( $nf->formatCurrency(1234.56789, $nf->getSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL)) ); // Output: string(14) "1 234,57 €"</code>
Time Zones and Calendars:
IntlTimeZone
manages time zones, mirroring functionalities of DateTimeZone
. IntlCalendar
provides a rich API for calendar operations:
<code class="language-php">$calendar = IntlCalendar::createInstance(); var_dump($calendar->getTimeZone()->getId()); // Output: Time zone ID (e.g., "UTC") $calendar = IntlCalendar::fromDateTime(new DateTime()); // Create from DateTime object // Comparisons $calendar1 = IntlCalendar::fromDateTime( DateTime::createFromFormat('j-M-Y', '11-Apr-2016') ); $calendar2 = IntlCalendar::createInstance(); $diff = $calendar1->fieldDifference($calendar2->getTime(), IntlCalendar::FIELD_MILLISECOND); // ... (comparison and date navigation examples as before)</code>
Date navigation is intuitive:
<code class="language-php">$calendar = IntlCalendar::createInstance(); $calendar->add(IntlCalendar::FIELD_MONTH, 1); // Add a month $calendar->add(IntlCalendar::FIELD_DAY_OF_WEEK, 1); // Add a day of the week // ...</code>
Conclusion:
The PHP Intl extension, powered by ICU, offers a powerful and comprehensive solution for internationalizing your PHP applications. This two-part series has covered message localization and now complex data localization. Future articles will explore additional functionalities within the Intl extension.
The above is the detailed content of Localizing Dates, Currency, and Numbers with Php-Intl. For more information, please follow other related articles on the PHP Chinese website!