搜尋
首頁後端開發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
unset()和session_destroy()有什麼區別?unset()和session_destroy()有什麼區別?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

在負載平衡的情況下,什麼是粘性會話(會話親和力)?在負載平衡的情況下,什麼是粘性會話(會話親和力)?May 04, 2025 am 12:16 AM

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

PHP中有哪些不同的會話保存處理程序?PHP中有哪些不同的會話保存處理程序?May 04, 2025 am 12:14 AM

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的會話是什麼?為什麼使用它們?PHP中的會話是什麼?為什麼使用它們?May 04, 2025 am 12:12 AM

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

說明PHP會話的生命週期。說明PHP會話的生命週期。May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

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

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。