https://github.com/ivanbeldad/php-dates
Basic Dates library for PHP
https://github.com/ivanbeldad/php-dates
Last synced: 6 months ago
JSON representation
Basic Dates library for PHP
- Host: GitHub
- URL: https://github.com/ivanbeldad/php-dates
- Owner: ivanbeldad
- License: mit
- Created: 2017-10-24T00:58:16.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-26T02:04:16.000Z (over 8 years ago)
- Last Synced: 2025-12-23T06:44:13.142Z (7 months ago)
- Language: PHP
- Size: 53.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Dates
[](https://packagist.org/packages/ivandelabeldad/dates)
[](https://github.com/ivandelabeldad/api-rackian/blob/master/LICENSE)
Basic Dates library for PHP
## Install
```
composer require ivandelabeldad/dates
```
## Usage
### Date, Time, and DateTime
```php
// Time only have hour, minute and seconds
$time = Time::now();
// Date have year, month, day, season and day of week
$date = Date::now();
// DateTime have date attributes and time either
$dateTime = DateTime::now();
```
### Create a Date, Time or DateTime
```php
// FROM YEAR-MONTH-DAY HOUR-MINUTE-SECOND
$time = Time::create(12, 45, 0);
$date = Date::create(1999, 12, 31);
$dateTime = DateTime::create(1999, 12, 31, 12, 45, 0);
// FROM CURRENT TIME
$time = Time::now();
$date = Date::now();
$dateTime = DateTime::now();
// FROM TIME IN SECONDS (UNIX TIME)
$time = Time::fromUnixTime(time());
$date = Date::fromUnixTime(time());
$dateTime = DateTime::fromUnixTime(time());
```
### Get a Date range (Only Dates)
```php
$startDate = Date::create(2000, 1, 1);
$endDate = Date::create(2000, 12, 31);
// Contains every day of the year 2000
$dates = DateUtils::datesBetween($startDate, $endDate);
```
### DateLists usage
```php
$list = new DateArrayList();
// ADD ONE DATE
$list->add(Date::now());
// ADD MULTIPLE DATES
$list->addAll([
$date1,
$date2,
$date3,
]);
// RETURN 4
$list->size();
// REMOVE FIRST DATE AND MOVE OTHERS TO THE BEGINNING
$list->remove(0);
```
### Sorting using lists
```php
// DATES SORTED FROM BEFORE TO AFTER
$list->sort(new DateNaturalComparator());
// DATES SORTED FROM AFTER TO BEFORE
$list->sort(new DateNaturalComparator(), false);
```
### Example of filtering usage
```php
// Get only weekends in summer and spring
$filtered = DateFilter::builder($listOfDates)
->filterBySeasons([Season::SUMMER, Season::SPRING])
->filterByDaysOfWeek([DayOfWeek::SATURDAY, DayOfWeek::SUNDAY])
->build();
```
## License
The API Rackian is open-sourced software licensed under
the [MIT LICENSE](https://github.com/ivandelabeldad/php-dates/blob/master/LICENSE)