Backend Development
PHP Problem
How to use the internationalization function in PHP to view currency and date informationHow to use the internationalization function in PHP to view currency and date information
做为一门在世界范围内广泛使用的编程语言,国际化能力往往是衡量一个编程语言是否能够大范围流行的重要内容。特别是对于PHP这种以Web页面编程为主战场的语言来说,国际化能力更是重中之重。今天我们简单的学习一下与国际化相关的扩展及函数库。

在 PHP 中,已经为我们准备好了很多国际化相关的扩展及函数库,今天我们先来简单的学习一下,后面在学习到具体的 国际化与字符编码支持 相关的函数库时再进行深入的讲解学习。
获取当前浏览器用户的所在地
首先,我们来获取当前访问用户的所在地信息,也就是我们常见的 zh-cn 、 en-us 这类的地区语言编码信息。
// $_SERVER['HTTP_ACCEPT_LANGUAGE'] = zh-CN,zh;q=0.9; $browserLocale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); print_r($browserLocale); echo '<br/>'; // zh
浏览器传递的这类区域信息是 zh-CN,zh;q=0.9; 这种形式的,我们可以通过 Locale::acceptFromHttp() 获得具体的区域信息。不过需要注意的是,不管是 zh-cn 还是 zh-tw ,返回的都是 zh ,而英文则会返回具体的是 en-us 还是 en-gd 之类的内容,也就是说这个函数对于我们中文语言的结果获取是有问题的。
$locale = ['en', 'fr-FR', 'da, en-gb;q=0.8, en;q=0.7', 'zh-cn', 'zh-tw', 'en-us', 'en-ca', 'ja-jp'];
foreach($locale as $code){
print_r(Locale::acceptFromHttp($code));
echo '<br/>';
}
// en
// fr_FR
// da
// zh
// zh
// en_US
// en_CA
// ja_JP我们可以手动来指定区域信息,可以看出除了 zh 之外其它的都是可以获取到正常的具体区域信息的。这点是我们使用这个函数时需要注意的地方。
获取指定地区所在地的标准数字信息
对于我们中国人来,数字比较习惯的是百、千、万位,一般也不太需要区分,而西方国家则喜欢三位一个逗号的分隔开来,也就是对应他们的英文数字习惯,比如 10000,在西方表示中是 10,000 ,英语是 ten thousand , 10000000 表示为 10,000,000 ,英语是 ten million 。因此,在 PHP 中,也是有相对于这种情况的数字表示形式,也就是为普通数字加上逗号以表示西方数字标准格式。
$locale = ['en', 'fr-FR', 'en-gb', 'zh-cn','en-us', 'ko-kr', 'ja-jp'];
$money = 1234567.89;
foreach($locale as $code){
$numberformat = new NumberFormatter($code, NumberFormatter::DECIMAL);
echo $code, ":";
echo $numberformat->format($money), ', ';
echo $numberformat->parse($numberformat->format($money)), '<br/>';
}
// en:1,234,567.89, 1234567.89
// fr-FR:1 234 567,89,
// en-gb:1,234,567.89, 1234567.89
// zh-cn:1,234,567.89, 1234567.89
// en-us:1,234,567.89, 1234567.89
// ko-kr:1,234,567.89, 1234567.89
// ja-jp:1,234,567.89, 1234567.89我们直接使用 format() 方法,就可以获得指定地区的标准数字表示形式。可以看出亚洲地区的都是和欧美国家相同的,但法国则是使用空格进行分隔的。
获取指定地区所在地的货币符号信息
既然有数字格式的不同了,那么货币形式的区别就也是类似的形式了,只不过我们在使用 NumberFormatter 类的时候需要指定后面的数字类型常量为货币形式 NumberFormatter::PERCENT 。
oreach($locale as $code){
$numberformat = new NumberFormatter($code, NumberFormatter::PERCENT);
echo $code, ":";
echo $numberformat->format($money), '<br/>';
}
// en:¤1,234,567.89
// fr-FR:1 234 567,89 €
// en-gb:£1,234,567.89
// zh-cn:¥1,234,567.89
// en-us:$1,234,567.89
// ko-kr:₩1,234,568
// ja-jp:¥1,234,568看出来了吗,当指定了货币形式后,format() 出来的结果直接就带上了货币符号。第一个 en 的货币符号其实就是国际通用货币符号,而日元和我们人民币的符号是相同的,这也是一个有趣的历史知识,大家可以自行查询下。
获取指定地区所在地的完整日期
最后就是获取的各地的日期标准格式。其实现在大家通用的都是 yyyy-mm-dd 这种形式来表示日期,不过正式规范的日期标准格式各个地区还是有所不同的。
$date = '2020-09-25 11:05:22';
foreach($locale as $code){
$l = new Locale($code);
$d = new IntlDateFormatter($code, IntlDateFormatter::FULL, IntlDateFormatter::FULL);
$c = IntlCalendar::createInstance(NULL, $code);
$c->set('2020', '09', '25', '11', '22', '33');
echo $code, ":";
echo $d->format($c), "<br/>";
}
// en:Friday, September 25, 2020 at 2:48:12 PM China Standard Time
// fr-FR:vendredi 25 septembre 2020 à 14:48:12 heure normale de la Chine
// en-gb:Friday, 25 September 2020 at 14:48:12 China Standard Time
// zh-cn:2020年9月25日星期五 中国标准时间 下午2:48:12
// en-us:Friday, September 25, 2020 at 2:48:12 PM China Standard Time
// ko-kr:2020년 9월 25일 금요일 오후 2시 48분 12초 중국 표준시
// ja-jp:2020年9月25日金曜日 14時48分12秒 中国標準時是不是很方便,直接就以当地的语言来表示指定地区的标准日期格式了。
总结
对于跨境项目来说,这些功能是非常重要的能力,能够帮助我们快捷地开发或切换相关的地区,等到深入学习的时候再进行详细的学习讲解,这次就全当是一次热身吧!
测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202009/source/11.学习PHP中的国际化功能来查看货币及日期信息.php
推荐学习:php视频教程
The above is the detailed content of How to use the internationalization function in PHP to view currency and date information. For more information, please follow other related articles on the PHP Chinese website!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.






