?
PHP库将日期转换为多个日历
安装此扩展程序的首选方法是通过作曲家。
要么运行
composer require --prefer-dist meysampg/intldate " * "或添加
"meysampg/intldate" : " * "要为composer.json文件的要求部分。
另外,您可以下载并使用它。
安装库后,只需在PHP文件中使用它:
use meysampg intldate IntlDateTrait ;并在您所需的课程上使用它:
<?php
namespace foo bar ;
use meysampg intldate IntlDateTrait ;
class Bar
{
use IntlDateTrait;
// Some codes are here!
echo $ this -> fromGregorian ([ 2017 , 9 , 7 , 12 , 23 , 45 ])-> toPersian ( ' en ' )-> asDateTime ();
}IntlDateTrait has a simple logic for naming methods: "A date starts from origin and ends to final . So all methods (setters and getters) that are related to incoming date are named by setOriginXXXX and getOriginXXXX (which XXXX shows a feature of time, like timezone or locale ) and all methods that are corresponded to outgoing date are regarded as setFinalXXXX and getFinalXXXX可以在本文档的底部找到可用方法的列表。
首先,我必须注意,即将到来的日期必须是此形式的数组:
[
year, // 2016
month, // 2
day, // 23
hour, // 12
minute, // 23
second // 4
]当前,库无法将字符串作为时间解析(请参见TODO部分),因此,在转换日期之前,您必须在可接受的数组上解析它(可以使用preg_match或您知道的每个工具来完成)。现在,您可以轻松地使用IntlDateTrait::from($datetimeArray, $locale, $calendar)来导入传入的日期和IntlDateTrait::to($locale, $calendar)将其转换为另一个系统。在本节的第一部分中,我们将大约$datetimeArray毫无疑问,这是我们以接受格式转换的日期。 $locale是一种语言的区域信息。例如,对于英语en ,对于Farsi来说,它是fa ,对于西班牙来说,它是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') 。如果您想通过拉丁字数字显示转换的日期(基于日历, $locales定义了$ convented日期Persian则在传入功能中使用$locale ,并在传出功能上使用fa 。同样,随着fromYyyy和ToZzzz功能中的$timezone ,您可以将DateTime从一个转换为另一个。速记在表中列出。
| 传入 | 向外 |
|---|---|
| 来自珀斯人 | 高层人士 |
| 从日本 | Tojapanese |
| 来自博客 | tobuddhist |
| 源自Chinese | tochinese |
| 从印第安人 | Toindian |
| 伊斯兰教 | toislamic |
| 来自Hebrew | 托堡 |
| 源 | 巨型 |
| 来自义工 | toethiopic |
| Fromgregorian | togregorian |
有两个用于显示转换日期的函数。第一个是asDateTime ,最后一个是asTimestamp 。
asDateTime的签名为asDateTime($pattern = 'yyyy/MM/dd, HH:mm:ss') 。此函数接受ICU可接受的格式。您可以从此链接中找到更多信息。同样,实施一个可以解析传统PHP date的函数的好主意(请参阅TODO部分)。
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' 有一些方法有助于更多地控制转换过程。我只是在本节中列出它们。我认为您的名字抱怨他们的用法,如果不是,请为他们写文件:d。
| 设定器 | Gettes |
|---|---|
setOriginDate($datetimeArray) | getFinalDate() |
setFromLocale($locale) | getFromLocale() |
setFromCalendar($calendar) | getFromCalendar() |
setToLocale($locale) | getToLocale() |
setToCalendar($calendar) | getToCalendar() |
getFromLocaleAndCalendar() | getToLocaleAndCalendar() |
setOriginTimeZone($timezone) | getOriginTimeZone() |
setFinalTimeZone($timezone) | getFinalTimeZone() |
setOriginCalendar($locale) | getOriginCalendar() |
setFinalCalendar($locale) | getFinalCalendar() |
setFinalDateType($datetype) | getFinalDateType() |
setFinalTimeType($timetype) | getFinalTimeType() |
setFinalCalendarType($calendarType) | getFinalCalendarType() |
setFinalPattern($pattern) | getFinalPattern() |
setIntlDateFormatter($locale = "en_US", $datetype = IntlDateFormatter::FULL, $timetype = IntlDateFormatter::FULL, $timezone = 'UTC', $calendar = IntlDateFormatter::GREGORIAN, $pattern = 'yyyy/MM/dd HH:mm:ss') | getIntlDateFormatter() |
setIntlCalendar($timezone = 'Asia/Tehran', $locale = 'fa_IR@calendar=persian') | getIntlCalendar() |
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)方法。
写测试!
仅分配此存储库,进行修改或添加,然后发送拉动请求!