jaybizzle/php-seasons
Composer 安装命令:
composer require jaybizzle/php-seasons
包简介
A small utility class that returns the season from a given date.
README 文档
README
A lightweight PHP utility class that returns the season from a given date. Supports meteorological and astronomical season calculations, northern and southern hemispheres, and custom season names for translations.
Installation
composer require jaybizzle/php-seasons
Quick Start
use Jaybizzle\Seasons; // Get the current season $season = new Seasons; $season->season(); // e.g. "Spring" // Get season for a specific date $season->season('1st June'); // "Summer" // Static convenience methods Seasons::now(); // e.g. "Spring" Seasons::for('1st June'); // "Summer"
Season Calculation Methods
By default, seasons are calculated using meteorological boundaries based on whole calendar months:
| Season | Months |
|---|---|
| Winter | December, January, February |
| Spring | March, April, May |
| Summer | June, July, August |
| Autumn | September, October, November |
Astronomical Seasons
You can switch to astronomical season boundaries, which use approximate equinox and solstice dates:
| Season | Approximate Dates |
|---|---|
| Spring | March 20 - June 20 |
| Summer | June 21 - September 22 |
| Autumn | September 23 - December 21 |
| Winter | December 22 - March 19 |
$season = new Seasons; $season->season('March 19'); // "Winter" (meteorological: March = Spring) $season->astronomical()->season('March 19'); // "Winter" (astronomical: before equinox) $season->astronomical()->season('March 21'); // "Spring" (astronomical: after equinox)
The astronomical calculation adjusts automatically for leap years.
Southern Hemisphere
Seasons in the southern hemisphere are reversed. Use the southern() method to get the correct season:
$season = new Seasons; $season->season('July'); // "Summer" $season->southern()->season('July'); // "Winter"
This works with astronomical mode too:
$season->astronomical()->southern()->season('March 25'); // "Autumn"
Immutable Fluent API
The astronomical() and southern() methods return a new instance, leaving the original unchanged. This means you can safely reuse the same base instance:
$season = new Seasons; $northern = $season->season('July'); // "Summer" $southern = $season->southern()->season('July'); // "Winter" $original = $season->season('July'); // Still "Summer"
Month Ranges
Get the month numbers that belong to a given season:
$season = new Seasons; $season->monthRange('Winter'); // [12, 1, 2] $season->monthRange('Summer'); // [6, 7, 8] // Southern hemisphere month ranges $season->southern()->monthRange('Summer'); // [12, 1, 2]
Translations
Pass an array of four season names to the constructor in the order: winter, spring, summer, autumn. All output will use your custom names:
// French $season = new Seasons(['Hiver', 'Printemps', 'Été', 'Automne']); $season->season('June'); // "Été" $season->season('January'); // "Hiver" $season->monthRange('Été'); // [6, 7, 8] // German $season = new Seasons(['Winter', 'Frühling', 'Sommer', 'Herbst']); $season->season('October'); // "Herbst" // Japanese $season = new Seasons(['冬', '春', '夏', '秋']); $season->season('April'); // "春"
Translations work with all features including southern(), astronomical(), and monthRange():
$french = new Seasons(['Hiver', 'Printemps', 'Été', 'Automne']); $french->southern()->season('June'); // "Hiver" $french->astronomical()->season('March 21'); // "Printemps"
The static methods also accept translations:
Seasons::for('June', ['Hiver', 'Printemps', 'Été', 'Automne']); // "Été" Seasons::now(['Hiver', 'Printemps', 'Été', 'Automne']); // e.g. "Printemps"
Date Formats
Any date string parsable by PHP's strtotime() is accepted:
$season = new Seasons; $season->season('June'); // "Summer" $season->season('1st October 2016'); // "Autumn" $season->season('2024-12-25'); // "Winter" $season->season('next Friday'); // depends on current date
An Exception is thrown if the date string cannot be parsed.
API Reference
| Method | Returns | Description |
|---|---|---|
new Seasons(?array $names) |
Seasons |
Create instance, optionally with custom season names |
season(?string $date) |
string |
Get season for date (or current date if null) |
astronomical() |
Seasons |
New instance using astronomical boundaries |
southern() |
Seasons |
New instance using southern hemisphere |
monthRange(string $season) |
array |
Get month numbers for a season |
Seasons::now(?array $names) |
string |
Static: get current season |
Seasons::for(string $date, ?array $names) |
string |
Static: get season for a date |
Constants
Seasons::SEASON_WINTER // "Winter" Seasons::SEASON_SPRING // "Spring" Seasons::SEASON_SUMMER // "Summer" Seasons::SEASON_AUTUMN // "Autumn"
License
MIT
jaybizzle/php-seasons 适用场景与选型建议
jaybizzle/php-seasons 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.6k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「date」 「season」 「seasons」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jaybizzle/php-seasons 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jaybizzle/php-seasons 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jaybizzle/php-seasons 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
A powerful event calendar Tool for Laravel's Nova 5.
Date component is a set of methods to help with the manipulation of dates.
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Temporal frequency library
统计信息
- 总下载量: 19.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-12