fahti/php-date-format

一个PHP库,用于将日期转换为文字表示

dev-main 2024-09-01 05:27 UTC

This package is auto-updated.

Last update: 2024-10-01 06:59:22 UTC


README

此库提供了一种简单灵活的方法,在PHP中将日期转换为文字表示。它处理各种日期格式,并提供了自定义的日期、月份和年份的输出。

特性

  • 将日期转换为文字表示
  • 支持多种输入日期格式
  • 处理日期的特殊情况(如1st、2nd、3rd、21st、22nd等)
  • 从1到9999年的文字表示一致
  • 广泛的测试套件以确保准确性

要求

  • PHP 7.4或更高版本
  • ext-intl扩展

安装

您可以通过Composer安装此包

composer require fahdi/php-date-format

用法

以下是如何使用DateToWords转换器的基本示例

<?php

require_once 'vendor/autoload.php';

use Fahdi\PhpDateFormat\DateToWords;

echo DateToWords::convert('1890-05-15');
// Output: Fifteenth of May, Eighteen ninety

echo DateToWords::convert('1990-05-15');
// Output: Fifteenth of May, Nineteen ninety

echo DateToWords::convert('2023-12-31');
// Output: Thirty-first of December, Twenty twenty-three

echo DateToWords::convert('1800-01-01');
// Output: First of January, Eighteen hundred

echo DateToWords::convert('1900-01-01');
// Output: First of January, Nineteen hundred

echo DateToWords::convert('2000-02-29');
// Output: Twenty-ninth of February, Two thousand

echo DateToWords::convert('2100-12-31');
// Output: Thirty-first of December, Twenty-one hundred

convert方法接受各种格式的日期

  • 'Y-m-d'(例如,'1990-05-15')
  • 'd-m-Y'(例如,'15-05-1990')
  • 'm/d/Y'(例如,'05/15/1990')
  • 'd/m/Y'(例如,'15/05/1990')
  • 'Y/m/d'(例如,'1990/05/15')
  • 'F j, Y'(例如,'May 15, 1990')
  • 'Y.m.d'(例如,'1990.05.15')

如果提供无效的日期格式,则方法将返回"无效的日期格式"。

运行测试

要运行测试,请在项目根目录中使用以下命令

./vendor/bin/phpunit tests

贡献

欢迎贡献!请随时提交Pull Request。

许可证

本项目是开源软件,许可协议为MIT。