定制 sunkan/dictus 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sunkan/dictus

最新稳定版本:2.7.0

Composer 安装命令:

composer require sunkan/dictus

包简介

Date utility classes

README 文档

README

Latest Version Software License

This is a small library to help with date formatting and keeping a consistent clock in a project

Usage

Using formatters

use Sunkan\Dictus\DateTimeFormat;
use Sunkan\Dictus\DateTimeFormatter;
use Sunkan\Dictus\LocalizedFormat;
use Sunkan\Dictus\LocalizedStrftimeFormatter;
use Sunkan\Dictus\LocalizedDateTimeFormatter;

LocalizedDateTimeFormatter::addLocaleFormat('sv_SE', new \Sunkan\Dictus\Locales\SvSe());

$jsonDateFormatter = new DateTimeFormatter(DateTimeFormat::JSON);
$localizedStrftimeFormatter = new LocalizedStrftimeFormatter('sv_SE', LocalizedFormat::DATETIME);
$localizedDateTimeFormatter = new LocalizedDateTimeFormatter('sv_SE', 'l d F [kl.] H:i');

$date = new DateTimeImmutable();

echo $jsonDateFormatter->format($date);
echo $localizedStrftimeFormatterFormatter->format($date); // 21.08.2023 16:26
echo $localizedDateTimeFormatter->format($date); // måndag 21 augusti kl. 16:26

Using localized formatters

We recommend using LocalizedDateTimeFormatter that just support translating of the regular date format syntax.

It will translate the following keys:

  • D short version of day name
  • l long version of day name
  • M short version of month name
  • F long version of month name

It also supports 6 custom keys that helps with common localized formats

Example for icelandic

'LT'   => 'H:i',    // 17:32
'LTS'  => 'H:i:s',  // 17:32:12
'L'    => 'd.m.Y',  // 06.12.2023
'LL'   => 'j. F Y', // 6. desember 2023
'LLL'  => 'j. F [kl.] H:i',     // 6. desember kl. 17:32
'LLLL' => 'l j. F Y [kl.] H:i', // miðvikudagur 6. desember 2023 kl. 17:32

Custom localization

All you need to do to create your own localization is to create a class that's implements LocaleFormat

final class CustomLocal implements \Sunkan\Dictus\LocaleFormat
 {
	public function formatChar(string $char, \DateTimeImmutable $dateTime): ?string
	{
		return match ($char) {
			'D' => 'short custom weekday name',
			'l' => 'long custom weekday name',
			'M' => null, // use english month names
			'F' => null, // use english month names
			default => null,
		};
	}

	public function resolveFormat(string $format): ?string
	{
		return match($format) {
			'LT' => 'H:i', // can be any valid date format string
			'LTS' => 'H:i:s', // can be any valid date format string
			'L' => 'd.m.Y', // can be any valid date format string
			'LL' => 'j. F Y', // can be any valid date format string
			'LLL' => 'j. F [kl.] H:i', // can be any valid date format string
			'LLLL' => 'l j. F Y [kl.] H:i', // can be any valid date format string
			default => null,
		};
	}
}

Clock

The library provides 3 clock implementations

SystemClock, FrozenClock and ProjectClock

SystemClock is just a wrapper around new DateTimeImmutable('now')

FrozenClock is a wrapper around a fixed DateTimeImmutable object

ProjectClock is wrapper around a ClockInterface implementation

统计信息

  • 总下载量: 14.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-27