-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2131298
commit eabbe8b
Showing
15 changed files
with
350 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Releases | ||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
changelog: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: conventional Changelog Action | ||
id: changelog | ||
uses: TriPSs/conventional-changelog-action@v3.18.0 | ||
with: | ||
github-token: ${{ secrets.github_token }} | ||
|
||
- name: create release | ||
uses: actions/create-release@v1 | ||
if: ${{ steps.changelog.outputs.skipped == 'false' }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.github_token }} | ||
with: | ||
tag_name: ${{ steps.changelog.outputs.tag }} | ||
release_name: ${{ steps.changelog.outputs.tag }} | ||
body: ${{ steps.changelog.outputs.clean_changelog }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## [1.0.4](https://github.com/baumrock/HumanDates/compare/v1.0.3...v1.0.4) (2023-06-13) | ||
|
||
|
||
### Bug Fixes | ||
|
||
* composer autoload setup ([58c8a54](https://github.com/baumrock/HumanDates/commit/58c8a54ef277d31e2598e49eb233942fb09af8a0)) | ||
|
||
|
||
|
||
## [1.0.3](https://github.com/baumrock/HumanDates/compare/v1.0.2...v1.0.3) (2023-06-13) | ||
|
||
|
||
### Bug Fixes | ||
|
||
* add autoload for composer ([91daf67](https://github.com/baumrock/HumanDates/commit/91daf6780e9999143c6ed470515eac7977342acd)) | ||
|
||
|
||
|
||
## [1.0.2](https://github.com/baumrock/HumanDates/compare/v1.0.1...v1.0.2) (2023-06-13) | ||
|
||
|
||
### Bug Fixes | ||
|
||
* remove dots from default rangePatterns ([a9ec76e](https://github.com/baumrock/HumanDates/commit/a9ec76ebb4823dc2a23c08b51766d4ea6295317d)) | ||
|
||
|
||
|
||
## [1.0.1](https://github.com/baumrock/HumanDates/compare/46fd51855a0c09628cbc31f606998172bb54f8e7...v1.0.1) (2023-06-13) | ||
|
||
|
||
### Bug Fixes | ||
|
||
* default patterns ([46fd518](https://github.com/baumrock/HumanDates/commit/46fd51855a0c09628cbc31f606998172bb54f8e7)) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
<?php | ||
|
||
namespace RockFrontend; | ||
|
||
use IntlDateFormatter; | ||
|
||
class HumanDates | ||
{ | ||
/* default range patterns */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 baumrock.com | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# HumanDates | ||
|
||
HumanDates is a PHP library that provides convenient methods for formatting dates as human-readable strings and generating human-friendly date ranges. It uses the `IntlDateFormatter` class to ensure accurate localization and formatting based on the specified locale. | ||
|
||
## Usage | ||
|
||
To get started, include the HumanDates library and create a new instance: | ||
|
||
```php | ||
// manual download | ||
require_once "/path/to/HumanDates.php"; | ||
|
||
// using composer | ||
// composer require baumrock/humandates | ||
require_once "/path/to/composer/autoload.php"; | ||
|
||
// create HumanDates instance | ||
$dates = new HumanDates(); | ||
echo $dates->format("2023-01-01"); // 1. Jan 2023 | ||
echo $dates->range("2023-01-01", "2023-01-03"); // 1. - 3. Jan 2023 | ||
``` | ||
|
||
## Why Use HumanDates? | ||
|
||
HumanDates provides several benefits and reasons to consider using it in your PHP projects: | ||
|
||
1. Simplified Date Formatting | ||
|
||
HumanDates simplifies the process of formatting dates in PHP. It offers a straightforward and intuitive API for formatting both single dates and date ranges, eliminating the need for complex and error-prone manual formatting. | ||
|
||
2. Localization Support | ||
|
||
HumanDates leverages the `IntlDateFormatter` class, ensuring accurate localization of date formats based on the specified locale. This allows you to generate date strings that are culturally appropriate and easily understood by users from different regions. | ||
|
||
3. Customizable Date Range Formats | ||
|
||
With HumanDates, you have the flexibility to define custom format patterns for date ranges. You can easily tailor the output to match your specific requirements, such as including or excluding certain date components, adjusting separators, or changing the order of elements. | ||
|
||
4. Replacement for Deprecated `strftime()` | ||
|
||
As of PHP 8, the `strftime()` function is deprecated and will be removed in PHP 9. HumanDates can serve as a replacement for `strftime()`, providing a modern and reliable alternative for formatting dates in PHP. | ||
|
||
5. MIT License | ||
|
||
HumanDates is released under the permissive MIT License, allowing you to freely use, modify, and distribute the library in your projects, both personal and commercial. | ||
|
||
By using HumanDates, you can enhance the user experience by presenting dates in a human-readable format, improve code maintainability, and ensure consistent and accurate date formatting across your application. | ||
|
||
Give HumanDates a try and simplify your date formatting needs with ease! | ||
|
||
## Formatting a Single Date | ||
|
||
You can format a single date using the `format()` method, which can serve as a replacement for the `strftime()` function which is deprecated in PHP8 and will be removed in PHP9: | ||
|
||
```php | ||
echo $dates->format("2023-01-01", "d.M.y"); // Output: 1.1.2023 | ||
``` | ||
|
||
The `format()` method accepts a date string or timestamp as the first parameter and an optional format pattern as the second parameter. The format pattern follows the syntax defined by the `IntlDateFormatter` class: [See here for a list of all available letters](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table). | ||
|
||
## Formatting a Date Range | ||
|
||
HumanDates provides a convenient way to format date ranges as well. Use the `range()` method by passing the start and end dates: | ||
|
||
```php | ||
// example using locale de_DE | ||
echo $dates->range("2023-01-01", "2023-02-03"); // Output: 1. Jan. - 3. Feb. 2023 | ||
echo $dates->range("2023-01-01", "2023-01-03"); // Output: 1. - 3. Jan. 2023 | ||
``` | ||
|
||
By default, the `range()` method uses predefined patterns to format the date range. However, you can also specify custom patterns by providing an array of patterns as the third parameter: | ||
|
||
```php | ||
// example using locale de_DE | ||
echo $dates->range("2023-01-01", "2023-01-03", [ | ||
'default' => ['d. MMMM y', ' bis ', 'd. MMMM y'], // 1. Januar 2023 bis 3. Februar 2024 | ||
'sameYear' => ['d. MMMM', ' bis ', 'd. MMMM y'], // 1. Januar bis 3. Februar 2023 | ||
'sameMonth' => ['d.', ' bis ', 'd. MMMM y'], // 1. bis 3. Januar 2023 | ||
'sameDay' => ['d. MMMM y'], // 1. Januar 2023 | ||
]); | ||
``` | ||
|
||
## Setting Locales | ||
|
||
You can set the locale for date formatting either globally for the current `HumanDates` instance or on a per-format basis. | ||
|
||
To set the locale globally, pass the desired locale as a parameter when creating a new `HumanDates` instance: | ||
|
||
```php | ||
$dates = new HumanDates("de_AT"); | ||
echo $dates->format($timestamp, "d. MMMM y"); // Output: 1. Jänner 2023 | ||
``` | ||
|
||
To set the locale for a specific `format()` call, include the `locale` parameter: | ||
|
||
```php | ||
echo $dates->format( | ||
$timestamp, | ||
format: "d. MMMM y", | ||
locale: "de_DE", | ||
); // Output: 1. Januar 2023 | ||
``` | ||
|
||
## Setting Defaults | ||
|
||
You can set default formatting options for your `HumanDates` instance by specifying the locale and format pattern during initialization: | ||
|
||
```php | ||
$dates = new HumanDates("de_AT", "dd.MM.y"); | ||
echo $dates->format("2023-1-1"); // Output: 01.01.2023 | ||
echo $dates->format("2023-1-1", "d. MMMM y"); // Output: 1. Jänner 2023 | ||
``` | ||
|
||
## Star the Repository | ||
|
||
If you find HumanDates useful or interesting, please star the repository on GitHub. By starring the repository, you show your appreciation and support for the project. It also helps us understand the level of community interest and motivates us to further improve the library. So, why not give us a star today? ⭐ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "baumrock/humandates", | ||
"description": "Format date ranges in a human-readable format with automatic pattern selection and localization support.", | ||
"require": { | ||
"php": ">=8.0" | ||
}, | ||
"license": "MIT", | ||
"autoload": { | ||
"files": [ | ||
"HumanDates.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": "1.0.4" | ||
} |
Oops, something went wrong.