搜索
首页后端开发php教程与PHP-INTL的本地化日期,货币和数字

>本教程基于对PHP INTL扩展的先前介绍,重点是本地化复杂数据(例如数字,日期和货币)。 让我们潜入!

Localizing Dates, Currency, and Numbers with Php-Intl

密钥概念:

  • php intl扩展程序利用ICU库进行鲁棒的环境意识数据处理,对于多语言应用程序至关重要。
  • NumberFormatter处理数字本地化,解决了小数分离器中的变化以及格式的不同地区的格式。
  • >使用NumberFormatter可以轻松实现货币格式,指定货币代码并使用formatCurrency> method。
  • 扩展程序为日期操作和比较提供了一个综合的日历API(
  • ),提供了与流行的日期/时间库相似的功能。IntlCalendar>

>十进制定位:

跨区域的小数分离器的不一致是一个普遍的挑战。

类优雅地解决了这一点:NumberFormatter>

$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) "১২,৩৪,৫৬,৭৮৯"
>语言环境代码(例如'de_de','en_us')指示格式化样式。 提供各种格式样式(十进制,货币,持续时间等)。

格式化样式和属性:

> 我们可以使用属性自定义数字格式:

可以控制

圆形行为:

$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"
前面介绍的

$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"
样式也在此处适用。 解析格式的字符串通过

方法支持SPELLOUT支持。 DURATION parse货币本地化:

格式数字作为货币很简单:>

方法简化了货币符号检索:
$nf = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );
var_dump( $nf->formatCurrency(1234.56789, "USD" ) ); // Output: string(9) ",234.57"

getSymbol

>时区和日历:
$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 €"

> >管理时区,镜像

的功能。

为日历操作提供了丰富的API:IntlTimeZone DateTimeZone IntlCalendar日期导航是直观的:

$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)

结论:
$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
// ...
由ICU提供支持的PHP INTL扩展名提供了一种强大而全面的解决方案,用于将您的PHP应用程序国际化。 这个两部分系列涵盖了消息本地化,现在复杂的数据本地化。未来的文章将探讨INTL扩展中的其他功能。

以上是与PHP-INTL的本地化日期,货币和数字的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
高流量网站的PHP性能调整高流量网站的PHP性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境