parsidev/date
Composer 安装命令:
composer require parsidev/date
包简介
A date library to help you work with dates in different languages
README 文档
README
This date library extends Carbon with multi-language support. Methods such as format, diffForHumans, parse, createFromFormat and the new timespan, will now be translated based on your locale.
Installation
Install using composer:
composer require parsidev/date
Laravel
There is a service provider included for integration with the Laravel framework. This provider will get the application locale setting and use this for translations. To register the service provider, add the following to the providers array in config/app.php:
Parsidev\Date\DateServiceProvider::class,
Languages
This package contains language files for the following languages:
- Arabic
- Azerbaijani
- Basque
- Brazilian Portuguese
- Bulgarian
- Catalan
- Croatian
- Chinese Simplified
- Chinese Traditional
- Czech
- Danish
- Dutch
- English
- Esperanto
- Finnish
- French
- German
- Greek
- Hebrew
- Hungarian
- Icelandic
- Indonesian
- Italian
- Japanese
- Korean
- Malay
- Norwegian
- Polish
- Portuguese
- Persian (Farsi)
- Romanian
- Russian
- Thai
- Serbian
- Slovak
- Slovenian
- Spanish
- Swedish
- Turkish
- Ukrainian
- Vietnamese
Usage
The Date class extends the Carbon methods such as format and diffForHumansn and translates them based on your locale:
use Parsidev\Date\Date; Date::setLocale('nl'); echo Date::now()->format('l j F Y H:i:s'); // zondag 28 april 2013 21:58:16 echo Date::parse('-1 day')->diffForHumans(); // 1 dag geleden
The Date class also added some aliases and additional methods such as: ago which is an alias for diffForHumans, and the timespan method:
echo $date->timespan(); // 3 months, 1 week, 1 day, 3 hours, 20 minutes
Methods such as parse and createFromFormat also support "reverse translations". When calling these methods with translated input, it will try to translate it to English before passing it to DateTime:
$date = Date::createFromFormat('D d F Y', 'zaterdag 21 maart 2015');
Carbon
Carbon is the library the Date class is based on. All of the original Carbon operations are still available, check out https://github.com/briannesbitt/Carbon for more information. Here are some of the available methods:
Creating dates
You can create Date objects just like the DateTime object (http://www.php.net/manual/en/datetime.construct.php):
$date = new Date(); $date = new Date('2000-01-31'); $date = new Date('2000-01-31 12:00:00'); // With time zone $date = new Date('2000-01-31', new DateTimeZone('Europe/Brussels'));
You can skip the creation of a DateTimeZone object:
$date = new Date('2000-01-31', 'Europe/Brussels');
Create Date objects from a relative format (http://www.php.net/manual/en/datetime.formats.relative.php):
$date = new Date('now'); $date = new Date('today'); $date = new Date('+1 hour'); $date = new Date('next monday');
This is also available using these static methods:
$date = Date::parse('now'); $date = Date::now();
Creating a Date from a timestamp:
$date = new Date(1367186296);
Or from an existing date or time:
$date = Date::createFromDate(2000, 1, 31); $date = Date::createFromTime(12, 0, 0); $date = Date::create(2000, 1, 31, 12, 0, 0);
Formatting Dates
You can format a Date object like the DateTime object (http://www.php.net/manual/en/function.date.php):
echo Date::now()->format('Y-m-d'); // 2000-01-31
The Date object can be cast to a string:
echo Date::now(); // 2000-01-31 12:00:00
Get a human readable output (alias for diffForHumans):
echo $date->ago(); // 5 days ago
Calculate a timespan:
$date = new Date('+1000 days'); echo Date::now()->timespan($date); // 2 years, 8 months, 3 weeks, 5 days // or even echo Date::now()->timespan('+1000 days');
Get years since date:
$date = new Date('-10 years'); echo $date->age; // 10 $date = new Date('+10 years'); echo $date->age; // -10
Manipulating Dates
You can manipulate by using the add and sub methods, with relative intervals (http://www.php.net/manual/en/datetime.formats.relative.php):
$yesterday = Date::now()->sub('1 day'); $tomorrow = Date::now()->add('1 day'); // ISO 8601 $date = Date::now()->add('P2Y4DT6H8M');
You can access and modify all date attributes as an object:
$date->year = 2013: $date->month = 1; $date->day = 31; $date->hour = 12; $date->minute = 0; $date->second = 0;
Contributing
You can easily add new languages by adding a new language file to the lang directory. These language entries support pluralization. By using a "pipe" character, you may separate the singular and plural forms of a string:
'hour' => '1 hour|:count hours', 'minute' => '1 minute|:count minutes', 'second' => '1 second|:count seconds',
Some languages have a different unit translation when they are used in combination with a suffix like 'ago'. For those situations you can add additional translations by adding the suffix to the unit as a key:
'year' => '1 Jahr|:count Jahre', 'year_ago' => '1 Jahr|:count Jahren',
There is also a generator.php script that can be used to quickly output day and month translations for a specific locale. If you want to add a new language, this can speed up the process:
php generator.php nl_NL
NOTE! If you are adding languages, please check the rules about the capitalization of month and day names: http://meta.wikimedia.org/wiki/Capitalization_of_Wiktionary_pages#Capitalization_of_month_names
License
Laravel Date is licensed under The MIT License (MIT).
parsidev/date 适用场景与选型建议
parsidev/date 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「translation」 「time」 「date」 「laravel」 「datetime」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 parsidev/date 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 parsidev/date 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 parsidev/date 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Tools to convert between .NET and PHP date formats
A custom URL rule class for Yii 2 which allows to create translated URL rules
Adds the EDTF data type to Wikibase
A Laravel Eloquent model trait for translatable resource
Date component is a set of methods to help with the manipulation of dates.
统计信息
- 总下载量: 110
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-19