janklan/simple-date-time
Composer 安装命令:
composer require janklan/simple-date-time
包简介
Timezone-agnostic Date and Time value objects for PHP
README 文档
README
Timezone-agnostic Date and Time value objects for PHP.
Why?
PHP's DateTime and DateTimeImmutable are powerful but carry timezone complexity that's unnecessary for many use cases:
- Birthdays - A birthday is January 15th, not "January 15th in UTC-5"
- Special Days - The Australia Day is on January 26th, The Independence Day is on July 4th - regardless of which time zone in Australia or the U.S. you are
- Business hours - Standard business hours across a global enterprise could be 9am to 5pm, regardless of where on Earth do you work for the business: you have to rock up at 9am for work, wherever you are.
When you want to store a date or time as a face value, without the time zone, you can find yourself fighting with the native \DateTimeInterface objects.
This package provides SimpleDateImmutable, SimpleDate, SimpleTimeImmutable, and SimpleTime classes that represent pure calendar dates and wall-clock times without timezone baggage.
Installation
composer require janklan/simple-date-time
Usage
Date
use JanKlan\SimpleDateTime\SimpleDateImmutable; $birthday = new SimpleDateImmutable('1990-05-15'); $today = SimpleDateImmutable::today(); $christmas = SimpleDateImmutable::fromString('2025-12-25'); // Comparisons $today->isBefore($christmas); // true $today->isAfter($birthday); // true $today->isSame($birthday); // false // Arithmetic $nextWeek = $today->modify('+1 week'); // Formatting (date characters only) echo $birthday->format('l, F j, Y'); // "Tuesday, May 15, 1990"
Time
use JanKlan\SimpleDateTime\SimpleTimeImmutable; $opening = new SimpleTimeImmutable('09:00:00'); $closing = SimpleTimeImmutable::create(17, 30, 0); $now = SimpleTimeImmutable::now(); $midnight = SimpleTimeImmutable::midnight(); $noon = SimpleTimeImmutable::noon(); // Comparisons $now->isBefore($closing); // true (during business hours) $now->isAfter($opening); // true $now->isSame($noon); // false // Arithmetic (wraps at midnight) $lateNight = SimpleTimeImmutable::create(23, 0, 0); $earlyMorning = $lateNight->modify('+2 hours'); // 01:00:00 // Formatting (time characters only) echo $opening->format('g:i A'); // "9:00 AM"
Mutable Variants
Both SimpleDate and SimpleTime mutable variants are available, though immutable versions are recommended:
use JanKlan\SimpleDateTime\SimpleDate; use JanKlan\SimpleDateTime\SimpleTime; $date = new SimpleDate('2025-01-15'); $date->modify('+1 day'); // Modifies in place $time = new SimpleTime('14:30:00'); $time->setTime(15, 0, 0); // Modifies in place
Blocked Operations
Operations that would leak time/timezone information throw exceptions:
// Date objects block time operations $date->setTime(12, 0, 0); // LogicException $date->setTimezone($tz); // LogicException $date->format('H:i:s'); // InvalidArgumentException $date->modify('+1 hour'); // LogicException // Time objects block date operations $time->setDate(2025, 1, 15); // LogicException $time->setTimezone($tz); // LogicException $time->format('Y-m-d'); // InvalidArgumentException $time->modify('+1 day'); // LogicException
PHPStan Integration
The package includes a PHPStan rule that detects comparison operators used on date/time objects. To enable it, add to your phpstan.neon:
includes: - vendor/janklan/simple-date-time/phpstan-extension.neon
This catches errors like:
$date1 < $date2 // Error: Use isBefore() instead $time1 == $time2 // Error: Use isSame() instead
Doctrine Integration
For database persistence, see janklan/simple-date-time-for-doctrine.
Contributing
PR are welcome. All you need to do to get started is to check out the repository, run composer install and you're done.
Several shorthand commands are configured in Composer: cs, phpstan, rector, test, and preflight. See composer.json to find out what they do.
Run composer preflight at least at the end of your work, before you create a pull request.
Code Coverage
Generate coverage reports in ./coverage:
composer test:cc
License
MIT
janklan/simple-date-time 适用场景与选型建议
janklan/simple-date-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「date」 「datetime」 「immutable」 「value-object」 「timezone-agnostic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 janklan/simple-date-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 janklan/simple-date-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 janklan/simple-date-time 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Time provider, providing consistent current date, time, datetime and timezone across the request.
Adds the EDTF data type to Wikibase
Datetime helpers for timezone handling
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
A powerful event calendar Tool for Laravel's Nova 5.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-08