An easy-to-use PHP package (and Laravel Facade) that turns dates and numbers into words or ordinal words.
Numbers and each part of the date can additionally be turned into ordinal words. (first, second, third)
- Carbon
- PHP 8.0+
composer require geoffreyrose/date-and-number-to-words
use DateAndNumberToWords\DateAndNumberToWords;
...
$words = new DateAndNumberToWords();
$carbon = Carbon::create(2023, 4, 1);
$words->words($carbon, 'Do of M, Y');
Laravel uses Package Auto-Discovery, which doesn't require you to manually add the ServiceProvider and Facade.
$words = DateAndNumberToWords::words(now(), 'Do of M, Y');
You can pass a Carbon object, DateTime object or an integer for most methods
Note all examples below use Plain PHP (use DateAndNumberToWords\DateAndNumberToWords) but can be swapped with Laravel Facade (DateAndNumberToWords)
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
Must be between 999999999999999999 and -999999999999999999
Only int
will return an ordinal word. If $ordinal
is true but $number
is float
a non-ordinal word will be returned.
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
The default language is en
Every method other than month
supports every language PHP does. PHP's native NumberFormatter
is being used to translate numbers to words.
For months, translations are handled by Carbon, which has translations for 270+ locales.
public function setLanguage(string $language): void
$words = new DateAndNumberToWords();
$words->setLanguage('en');