hossein142001 / intldate
一个小型php库,用于在多个日历之间转换日期
Requires
- ext-intl: *
This package is not auto-updated.
Last update: 2024-09-19 14:04:00 UTC
README
PHP库,用于将日期转换为多个日历
安装
安装此扩展的首选方式是通过 composer。
运行
composer require --prefer-dist hossein142001/intldate "*"
或添加
"hossein142001/intldate": "*"
到您的 composer.json
文件的 require 部分。
您也可以轻松下载 并使用它。
用法
安装库后,只需在您的php文件中使用它
use hossein142001\intldate\IntlDateTrait;
并在您所需的类中使用它
<?php namespace foo\bar; use hossein142001\intldate\IntlDateTrait; class Bar { use IntlDateTrait; // Some codes are here! echo $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toPersian('en')->asDateTime(); }
结构
IntlDateTrait
的命名方法很简单:日期从 origin 开始,到 final 结束。因此,所有与传入日期相关的(设置器和获取器)方法都命名为 setOriginXXXX
和 getOriginXXXX
(其中 XXXX
显示时间的功能,如 timezone 或 locale),所有与传出日期对应的方法都视为 setFinalXXXX
和 getFinalXXXX
。可在本文档底部找到可用方法的列表。
转换
首先,我必须指出,传入的日期必须以以下形式为数组
[ year, // 2016 month, // 2 day, // 23 hour, // 12 minute, // 23 second // 4 ]
目前,库 不能 将字符串解析为时间(见待办事项部分),因此,在转换日期之前,您 必须 在可接受的数组上解析它(可以使用 preg_match
或您所知道的任何工具)。现在,您可以使用 IntlDateTrait::from($datetimeArray, $locale, $calendar)
轻松导入传入日期,并使用 IntlDateTrait::to($locale, $calendar)
将其转换为另一个系统。我们讨论了 $datetimeArray
的第一部分,这是我们转换的接受格式。 $locale
是语言的区域信息。例如,对于 English 它是 en
,对于 Farsi 它是 fa
,对于 Spanish 它是 es
等等。您可以在 此链接 中找到它们的完整列表。最后,日历是您所需的日期系统。此库使用 php
的 Intl*
家族,因此您可以使用 ICU
项目中支持的任何日历。现在支持的日历有
- 波斯
- 公历
- 日本
- 佛教
- 中国
- 印度
- 伊斯兰
- 希伯来
- 科普特
- 埃塞俄比亚
最好在代码中使用它们的处理器而不是直接使用名称。这些处理器是
// Use them in `self::$CAL_CALENDAR, for example `$calendar = self::$CAL_HEBREW`. $CAL_PERSIAN $CAL_JAPANESE $CAL_BUDDHIST $CAL_CHINESE $CAL_INDIAN $CAL_ISLAMIC $CAL_HEBREW $CAL_COPTIC $CAL_ETHIOPIC $CAL_GREGORIAN
简写
有一些简写可以轻松地转换日期(是的!我是个 懒惰的人 :D)。这些简写格式为 fromYyyy()
用于传入日期函数和 toZzzz()
用于传出日期函数,其中 Yyyy
和 Zzzz
是日历的名称。对于传入函数,签名是 fromYyyy($datetimeArray, $locale = 'en_US', $timezone = 'UTC')
,对于传出函数是 toZzzz($locale = 'fa', $timezone = 'UTC')
。在传入函数中使用 $locale
如果您有非拉丁数字,在传出函数中使用它,如果您想用拉丁数字显示转换后的日期(基于日历的区域,$locales
已定义,例如,传出函数中波斯日历的默认位置是 fa
)。此外,通过更改 fromYyyy
和 ToZzzz
函数中的 $timezone
,您可以将一个日期时间从一种转换为另一种。简写列表如下。
显示日期
显示转换日期有两种函数。第一个是asDateTime
,最后一个则是asTimestamp
。
asDateTime
函数的签名是asDateTime($pattern = 'yyyy/MM/dd, HH:mm:ss')
。此函数接受ICU可接受的格式。更多信息可以参考此链接。同时,实现一个解析传统PHP date
可接受格式的函数也是一个好主意(参见待办事项部分)。
asTimestamp
函数返回Unix纪元,之后的时间为正整数,之前的时间为负整数。
示例
有一些使用IntlDateTrait
的示例。我认为它们是必要的,也是足够的。
$this->fromTimestamp(1504770825)->toPersian('en')->asDateTime(); // '1396/06/16, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toPersian('en')->asDateTime(); // '1396/06/16, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toPersian()->asDateTime(); // '۱۳۹۶/۰۶/۱۶, ۰۷:۵۳:۴۵' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toJapanese()->asDateTime(); // '0029/09/07, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toIslamic()->asDateTime(); // '١٤٣٨/١٢/١٧, ٠٧:٥٣:٤٥' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toBuddhist()->asDateTime(); // '2560/09/07, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toChinese()->asDateTime(); // '0034/07/17, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toIndian()->asDateTime(); // '1939/06/16, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toHebrew()->asDateTime(); // 'תשע״ז/י״ב/ט״ז, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toCoptic()->asDateTime(); // '1733/13/02, 07:53:45' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->toEthiopic()->asDateTime(); // '2009/13/02, 07:53:45' $this->fromPersian([1396, 6, 16, 12, 23, 45])->toIslamic()->asDateTime(); // '١٤٣٨/١٢/١٧, ٠٧:٥٣:٤٥' $this->fromPersian([1396, 6, 16, 12, 23, 45])->toGregorian()->asDateTime(); // '2017/09/07, 07:53:45' $this->fromPersian([1396, 6, 16, 12, 23, 45])->toGregorian()->setFinalTimeZone('Asia/Tehran')->asDateTime(); // '2017/09/07, 12:23:45' $this->fromPersian([1396, 6, 16, 12, 23, 45])->toGregorian()->setFinalTimeZone('Asia/Tehran')->asDateTime('yyyy'); // '2017' $this->fromGregorian([2017, 9, 7, 12, 23, 45])->asTimestamp(); // '1504770825' $this->fromPersian([1396, 6, 16, 12, 23, 45])->asTimestamp(); // '1504770825'
IntlDateTrait API
有一些方法可以帮助控制转换过程。我将在本节中列出它们。我相信它们的名字反映了它们的用途,如果不是,请为它们编写文档 :D。
待办事项
- 实现
parsePattern($pattern)
方法。
/** * Implement a function to parse both ICU patterns and php date * function patterns and return a pattern that is compatible on * ICU format. The php pattern must start with php keyword, for * example `php:Y-F-d, H:i:s` is a php pattern. */
-
实现guessDateTime($timestring)
方法。 -
编写测试!
贡献
只需fork这个存储库,进行修改或添加,然后发送一个pull request即可!