geoffreyrose/ date-and-number-to-words
将 PHP 日期、Carbon 对象和数字转换为文字
v1.2.0
2024-09-04 07:44 UTC
Requires
- php: ^8.0
- ext-intl: *
- nesbot/carbon: ^2.66|^3.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.5
- phpunit/phpunit: ^9.0|^10.0|^11.0
This package is auto-updated.
Last update: 2024-09-04 07:45:49 UTC
README
日期和数字转换为标准文字或序数文字
一个易于使用的 PHP 包,可以将日期和数字转换为文字或序数文字。
数字和日期的每个部分还可以转换为序数文字。(第一、第二、第三)
要求
- Carbon
- PHP 8.0+
用法
使用 Composer
$ composer require geoffreyrose/date-and-number-to-words
<?php require 'vendor/autoload.php'; use DateAndNumberToWords\DateAndNumberToWords;
不使用 Composer
<?php require 'path/to/geoffreyrose/DateAndNumberToWords.php'; use DateAndNumberToWords\DateAndNumberToWords;
方法
您可以为大多数方法传递 Carbon 对象、DateTime 对象或整数
日期转换为文字
public function words(Carbon|DateTime $date, string $format): string $words = new DateAndNumberToWords(); $carbon = Carbon::create(2023, 4, 1); $words->words($carbon, 'Do of M, Y'); // first of April, two thousand twenty-three
格式
Yo : Ordinal Year - year($year, true)
Y : Year - year($year)
Mo : Ordinal Month - month($month, true)
M : Month - month($month)
Do : Ordinal Day - day($day, true)
D : Day - day($day)
Ho : Ordinal Hour - hour($hour, true)
H : Hour - hour($hour)
Io : Ordinal Minute - minute($minute, true)
I : Minute - minute($minute)
So : Ordinal Second - second($second, true)
S : Second - second($second)
年转换为文字
public function year(int|Carbon|DateTime $year, bool $ordinal = false): string $words = new DateAndNumberToWords(); $carbon = Carbon::create(2023, 4, 1); $dateTime = new DateTime(); $dateTime->setDate(2023, 4, 1); $date = new DateTime(); $words->year($carbon, true); // two thousand twenty-third $words->year($dateTime); // two thousand twenty-three $words->year(2023, true); // two thousand twenty-third $words->year(2023); // two thousand twenty-three
月转换为文字
public function month(int|Carbon|DateTime $month, bool $ordinal = false): string $words = new DateAndNumberToWords(); $words->month(4, true); // fourth $words->month(4); // April
日转换为文字
public function day(int|Carbon|DateTime $day, bool $ordinal = false): string $words = new DateAndNumberToWords(); $words->day(7, true); // seventh $words->day(7); // seven
小时转换为文字
public function hour(int|Carbon|DateTime $hour, bool $ordinal = false): string $words = new DateAndNumberToWords(); $words->hour(7, true); // seventh $words->hour(7); // seven
分钟转换为文字
public function minute(int|Carbon|DateTime $minute, bool $ordinal = false): string $words = new DateAndNumberToWords(); $words->minute(7, true); // seventh $words->minute(7); // seven
秒转换为文字
public function second(int|Carbon|DateTime $second, bool $ordinal = false): string $words = new DateAndNumberToWords(); $words->second(7, true); // seventh $words->second(7); // seven
数字转换为文字
必须介于 999999999999999999 和 -999999999999999999 之间
只有 int
会返回序数文字。如果 $ordinal
为 true 但 $number
是 float
,则返回非序数文字。
public function number(int|float $number, bool $ordinal = false): string $words = new DateAndNumberToWords(); $words->number(7, true); // seventh $words->number(7); // seven $words->number(24.68); // twenty-four point six eight $words->number(24.68, true); // twenty-four point six eight $words->number(999999999999999999) // nine hundred ninety-nine quadrillion nine hundred ninety-nine trillion nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine
设置语言
默认语言是 en
除了 month
以外的每个方法都支持 PHP 支持的所有语言。PHP 的原生 NumberFormatter
用于将数字转换为文字。
对于月份,由 Carbon 处理翻译,Carbon 为 270 多个区域提供了翻译。
public function setLanguage(string $language): void $words = new DateAndNumberToWords(); $words->setLanguage('en');