{"id":16233566,"url":"https://github.com/fre5h/datetime-php","last_synced_at":"2026-03-14T22:36:33.923Z","repository":{"id":45178980,"uuid":"190854938","full_name":"fre5h/datetime-php","owner":"fre5h","description":"🐘 🕒 📅 PHP library that provides additional functions for processing dates \u0026 times.","archived":false,"fork":false,"pushed_at":"2024-09-11T18:32:37.000Z","size":147,"stargazers_count":15,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-11T13:12:54.078Z","etag":null,"topics":["date","datetime","library","midnight","php","time","timezone"],"latest_commit_sha":null,"homepage":"https://github.com/fre5h/datetime-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fre5h.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"fre5h"}},"created_at":"2019-06-08T06:42:39.000Z","updated_at":"2024-09-11T18:30:35.000Z","dependencies_parsed_at":"2023-11-14T18:47:08.005Z","dependency_job_id":"a9a6a630-0bac-4c1c-a8e4-823ca31abf22","html_url":"https://github.com/fre5h/datetime-php","commit_stats":{"total_commits":77,"total_committers":1,"mean_commits":77.0,"dds":0.0,"last_synced_commit":"39fd4b8cbdf6000efaf8fb11052eeaa83f063f0f"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fre5h%2Fdatetime-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fre5h%2Fdatetime-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fre5h%2Fdatetime-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fre5h%2Fdatetime-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fre5h","download_url":"https://codeload.github.com/fre5h/datetime-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221663626,"owners_count":16859871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["date","datetime","library","midnight","php","time","timezone"],"created_at":"2024-10-10T13:13:06.531Z","updated_at":"2026-03-14T22:36:28.891Z","avatar_url":"https://github.com/fre5h.png","language":"PHP","funding_links":["https://github.com/sponsors/fre5h"],"categories":[],"sub_categories":[],"readme":"# DateTime\n\nPHP library that provides additional functions for processing dates \u0026 times. 🐘 🕒 📅\n\n[![Scrutinizer Quality Score](https://img.shields.io/scrutinizer/g/fre5h/datetime-php.svg?style=flat-square)](https://scrutinizer-ci.com/g/fre5h/datetime-php/)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/fre5h/datetime-php/ci.yaml?branch=main\u0026style=flat-square)](https://github.com/fre5h/datetime-php/actions?query=workflow%3ACI+branch%3Amain+)\n[![CodeCov](https://img.shields.io/codecov/c/github/fre5h/datetime-php.svg?style=flat-square)](https://codecov.io/github/fre5h/datetime-php)\n[![License](https://img.shields.io/packagist/l/fresh/datetime.svg?style=flat-square)](https://packagist.org/packages/fresh/datetime)\n[![Latest Stable Version](https://img.shields.io/packagist/v/fresh/datetime.svg?style=flat-square)](https://packagist.org/packages/fresh/datetime)\n[![Total Downloads](https://img.shields.io/packagist/dt/fresh/datetime.svg?style=flat-square)](https://packagist.org/packages/fresh/datetime)\n[![StyleCI](https://styleci.io/repos/190854938/shield?style=flat-square)](https://styleci.io/repos/190854938)\n[![Gitter](https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg?style=flat-square)](https://gitter.im/fre5h/datetime-php)\n\n## Requirements\n\n* PHP 8.3\n\n## Installation 🌱\n\n```composer req fresh/datetime```\n\n## Features 🎁\n\n### Popular time constants\n\nNumber of seconds in a minute, number of minutes in an hour, etc.\n\n```php\nuse Fresh\\DateTime\\TimeConstants;\n\necho TimeConstants::NUMBER_OF_SECONDS_IN_AN_HOUR; // etc.\n```\n\n### Methods for creating current `\\DateTime` and `\\DateTimeImmutable` objects (convenient for testing)\n\nIf you use separate class for creating datetime objects, you can mock these methods in your code and have the expected `\\DateTime` object what you need.\n\n```php\nuse Fresh\\DateTime\\DateTimeHelper;\n\n$dateTimeHelper = new DateTimeHelper();\n\n$now1 = $dateTimeHelper-\u003egetCurrentDatetime();\n$now2 = $dateTimeHelper-\u003egetCurrentDatetime(new \\DateTimeZone('Europe/Kyiv')); // Or with custom timezone\n$now3 = $dateTimeHelper-\u003egetCurrentDatetimeUtc(); // Always in UTC\n$now4 = $dateTimeHelper-\u003egetCurrentDatetimeImmutable();\n$now5 = $dateTimeHelper-\u003egetCurrentDatetimeImmutable(new \\DateTimeZone('Europe/Kyiv')); // Or with custom timezone\n$now6 = $dateTimeHelper-\u003egetCurrentDatetimeImmutableUtc(); // Always in UTC\n```\n\nCompatible with [PSR-20: Clock](https://www.php-fig.org/psr/psr-20/).\n\n```php\nuse Fresh\\DateTime\\DateTimeHelper;\n\n$dateTimeHelper = new DateTimeHelper();\n\n$now = $dateTimeHelper-\u003enow(); // \\DateTimeImmutable in UTC\n```\n\n### Method for getting current timestamp\n\n```php\nuse Fresh\\DateTime\\DateTimeHelper;\n\n$dateTimeHelper = new DateTimeHelper();\n\n$timestamp = $dateTimeHelper-\u003egetCurrentTimestamp();\n\n```\n\n### Method for creating `\\DateTime` or `\\DateTimeImmutable` from format\n\n```php\nuse Fresh\\DateTime\\DateTimeHelper;\n\n$dateTimeHelper = new DateTimeHelper();\n\n// By default with format Y-m-d H:i:s and UTC timezone\n$dateTime = $dateTimeHelper-\u003ecreateDateTimeFromFormat('2000-01-01 00:00:00');\n$dateTimeImmutable = $dateTimeHelper-\u003ecreateDateTimeImmutableFromFormat('2000-01-01 00:00:00');\n\n// With custom format\n$dateTime = $dateTimeHelper-\u003ecreateDateTimeFromFormat('01.01.2000 00:00:00', 'd.m.Y H:i:s');\n\n// With custom timezone\n$dateTime = $dateTimeHelper-\u003ecreateDateTimeFromFormat('01.01.2000 00:00:00', 'd.m.Y H:i:s', new \\DateTimeZone('Europe/Kyiv'));\n```\n\n### Method for creating `\\DateTimeZone` object\n\nIf you create a `\\DateTimeZone` object directly in your code, you will not be able to mock it in tests.\nSo there is a specific method for creating timezone object.\n\n```php\nuse Fresh\\DateTime\\DateTimeHelper;\n\n$dateTimeHelper = new DateTimeHelper();\n\n$dateTimeZone1 = $dateTimeHelper-\u003ecreateDateTimeZone(); // UTC by default\n$dateTimeZone2 = $dateTimeHelper-\u003ecreateDateTimeZone('Europe/Kyiv'); // Or with custom timezone\n$dateTimeZone3 = $dateTimeHelper-\u003ecreateDateTimeZoneUtc(); // Another method to get UTC timezone\n```\n\n### Immutable `DateRange` ValueObject\n\nYou often needed to manipulate with since/till dates, so-called date ranges.\nBy its nature, date range is a `ValueObject`, it can be reused many times for different purposes.\nThis library provides a `DateRange` immutable class, which is not able to be changed after its creation.\n`DateRange` operates only with dates and ignore time.\n\n```php\nuse Fresh\\DateTime\\DateRange;\n\n$dateRange1 = new DateRange(new \\DateTime('yesterday'), new \\DateTime('tomorrow'));\n$dateRange2 = new DateRange(new \\DateTime('yesterday'), new \\DateTime('tomorrow', new \\DateTimeZone('Europe/Kyiv')));\n\n// There is also the `isEqual` method to compare two DateRange objects.\n$dateRange1-\u003eisEqual($dateRange2); // Returns FALSE, because date ranges have different timezones\n```\n\n### Immutable `DateTimeRange` ValueObject\n\nThis library provides  also immutable class `DateTimeRange`, instead of `DateRange` it checks date and time.\n\n```php\nuse Fresh\\DateTime\\DateTimeRange;\n\n$dateTimeRange1 = new DateTimeRange(new \\DateTime('2000-01-01 19:00:00'), new \\DateTime('2000-01-01 21:00:00'));\n$dateTimeRange2 = new DateTimeRange(new \\DateTime('2000-01-01 19:00:00'), new \\DateTime('2000-01-01 21:00:00', new \\DateTimeZone('Europe/Kyiv')));\n$dateTimeRange3 = new DateTimeRange(new \\DateTime('2000-01-01 20:00:00'), new \\DateTime('2000-01-01 22:00:00'));\n\n// There is also the `isEqual` method to compare two DateTimeRange objects.\n$dateTimeRange1-\u003eisEqual($dateTimeRange2); // Returns FALSE, because datetime ranges have different timezones\n\n// There is also the `intersects` method to check if datetime range intersected each other.\n$dateTimeRange1-\u003eintersects($dateTimeRange3); // Returns TRUE, because datetime ranges are intersected\n```\n\n#### Examples of date ranges with intersection\n\n![Example of intersection](docs/images/intersect.png \"Example of intersection\")\n\n#### Examples of date ranges without intersection\n\n![Example of no intersection](docs/images/does_not_intersect.png \"Example of no intersection\")\n\n### Getting array of objects/strings of all dates in date range\n\n```php\nuse Fresh\\DateTime\\DateTimeHelper;\nuse Fresh\\DateTime\\DateRange;\n\n$dateTimeHelper = new DateTimeHelper();\n\n$dateRange = new DateRange(new \\DateTime('1970-01-01'), new \\DateTime('1970-01-03'));\n\n// Creates array with values ['1970-01-01', '1970-01-02', '1970-01-03']\n$datesAsStrings = $dateTimeHelper-\u003egetDatesFromDateRangeAsArrayOfStrings($dateRange);\n\n// Creates array of \\DateTime objects for dates: '1970-01-01', '1970-01-02', '1970-01-03'\n$datesAsObjects = $dateTimeHelper-\u003egetDatesFromDateRangeAsArrayOfObjects($dateRange);\n```\n\n### DateTimeCloner allows to clone dates into `\\DateTime` or `\\DateTimeImmutable` instances\n\n```php\nuse Fresh\\DateTime\\DateTimeCloner;\n\n$dateTimeCloner = new DateTimeCloner();\n\n$date1 = new \\DateTime();\n$dateImmutable1 = $dateTimeCloner::cloneIntoDateTimeImmutable($date1); // Returns \\DateTimeImmutable object\n$date2 = $dateTimeCloner::cloneIntoDateTime($dateImmutable1); // Returns \\DateTime object\n```\n\n## Contributing 🤝\n\nSee [CONTRIBUTING](https://github.com/fre5h/datetime-php/blob/master/.github/CONTRIBUTING.md) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffre5h%2Fdatetime-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffre5h%2Fdatetime-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffre5h%2Fdatetime-php/lists"}