{"id":15022849,"url":"https://github.com/kusabi/datetime","last_synced_at":"2026-02-14T09:07:05.429Z","repository":{"id":49235253,"uuid":"311587431","full_name":"kusabi/datetime","owner":"kusabi","description":"An extension of PHP's date time libraries","archived":false,"fork":false,"pushed_at":"2022-08-21T12:27:32.000Z","size":82,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-07T16:58:39.181Z","etag":null,"topics":["date","dates","datetime","extension","library","php","php5","php7"],"latest_commit_sha":null,"homepage":"","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/kusabi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":null,"code_of_conduct":".github/code_of_conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-10T08:05:37.000Z","updated_at":"2022-03-24T11:02:06.000Z","dependencies_parsed_at":"2022-07-30T07:17:58.692Z","dependency_job_id":null,"html_url":"https://github.com/kusabi/datetime","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/kusabi/datetime","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kusabi%2Fdatetime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kusabi%2Fdatetime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kusabi%2Fdatetime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kusabi%2Fdatetime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kusabi","download_url":"https://codeload.github.com/kusabi/datetime/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kusabi%2Fdatetime/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29441159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T07:24:13.446Z","status":"ssl_error","status_checked_at":"2026-02-14T07:23:58.969Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","dates","datetime","extension","library","php","php5","php7"],"created_at":"2024-09-24T19:58:27.082Z","updated_at":"2026-02-14T09:07:05.413Z","avatar_url":"https://github.com/kusabi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Datetime\n\n![Tests](https://github.com/kusabi/datetime/workflows/tests/badge.svg)\n[![codecov](https://codecov.io/gh/kusabi/datetime/branch/main/graph/badge.svg)](https://codecov.io/gh/kusabi/datetime)\n[![Licence Badge](https://img.shields.io/github/license/kusabi/datetime.svg)](https://img.shields.io/github/license/kusabi/datetime.svg)\n[![Release Badge](https://img.shields.io/github/release/kusabi/datetime.svg)](https://img.shields.io/github/release/kusabi/datetime.svg)\n[![Tag Badge](https://img.shields.io/github/tag/kusabi/datetime.svg)](https://img.shields.io/github/tag/kusabi/datetime.svg)\n[![Issues Badge](https://img.shields.io/github/issues/kusabi/datetime.svg)](https://img.shields.io/github/issues/kusabi/datetime.svg)\n[![Code Size](https://img.shields.io/github/languages/code-size/kusabi/datetime.svg?label=size)](https://img.shields.io/github/languages/code-size/kusabi/datetime.svg)\n\n\u003csup\u003eA quality of life extension for PHPs datetime libraries\u003c/sup\u003e\n\n## Compatibility and dependencies\n\nThis library is compatible with PHP version `7.2`, `7.3`, `7.4`, `8.0` and `8.1`.\n\nThis library has no dependencies.\n\n## Installation\n\nInstallation is simple using composer.\n\n```bash\ncomposer require kusabi/datetime\n```\n\nOr simply add it to your `composer.json` file\n\n```json\n{\n    \"require\": {\n        \"kusabi/datetime\": \"^1.0\"\n    }\n}\n```\n\n## Contributing\n\nThis library follows [PSR-1](https://www.php-fig.org/psr/psr-1/) \u0026 [PSR-2](https://www.php-fig.org/psr/psr-2/) standards.\n\n\n#### Unit Tests\n\nBefore pushing any changes, please ensure the unit tests are all passing.\n\nIf possible, feel free to improve coverage in a separate commit.\n\n```bash\nvendor/bin/phpunit\n```\n\n#### Code sniffer\n\nBefore pushing, please ensure you have run the code sniffer. **Only run it using the lowest support PHP version (5.6)**\n\n```bash\nvendor/bin/php-cs-fixer fix\n```\n\n#### Static Analyses\n\nBefore pushing, please ensure you have run the static analyses tool.\n\n```bash\nvendor/bin/phan\n```\n\n#### Benchmarks\n\nBefore pushing, please ensure you have checked the benchmarks and ensured that your code has not introduced any slowdowns.\n\nFeel free to speed up existing code, in a separate commit.\n\nFeel free to add more benchmarks for greater coverage, in a separate commit.\n\n```bash\nvendor/bin/phpbench run --report=quick\nvendor/bin/phpbench run --report=quick --output=markdown\nvendor/bin/phpbench run --report=quick --filter=benchNetFromTax --iterations=50 --revs=50000\n\nvendor/bin/phpbench xdebug:profile\nvendor/bin/phpbench xdebug:profile --gui\n```\n\n\n## Documentation\n\nThis section covers documentation for using the datetime library.\n\nThe library extends each of the native PHP datetime classes to make each of them a little more useful and user-friendly.\n\n### Examples of whole library\n\n```php\nuse Kusabi\\Date\\DateTime;\nuse Kusabi\\Date\\DatePeriod;\nuse Kusabi\\Date\\DateInterval;\n\n// Tomorrow end of working day\n$date = DateTime::tomorrow()-\u003esetTime(5,0,0);\n\n// Password expires in 30 days\n$date = DateTime::today()-\u003eaddDays(30);\n\n// Password expires in 30 days (end of day)\n$date = DateTime::now()-\u003eaddDays(30)-\u003eendOfDay();\n\n// Get the number of days between two dates\n$from = DateTime::createFromFormat('Y-m-d', '2000-01-01');\n$to = DateTime::createFromFormat('Y-m-d', '2003-05-01');\n$days = $from-\u003ediff($to)-\u003egetDays(); // 1120\n\n// Get all the dates between two dates\n$from = DateTime::createFromFormat('Y-m-d', '2022-01-01');\n$to = DateTime::createFromFormat('Y-m-d', '2022-01-10');\n$dates = DatePeriod::instance($from, DateInterval::day(), $to)-\u003egetDateTimes();\n$dates = $from-\u003egetDateTimesTo($to);\n```\n\n### Using the DateTime class\n\nThe `DateTime` class has been extended to add new ways of creating, reading, modifying and converting the instance.\n\n#### Creating DateTime instances\n\n```php\nuse Kusabi\\Date\\DateTime;\nuse Kusabi\\Date\\DateTimeZone;\n\n// Create a basic instance\n$date = new DateTime();\n$date = new DateTime('monday');\n$date = new DateTime('tomorrow');\n\n// Create an instance with a chain-able static method\n$date = DateTime::instance();\n$date = DateTime::instance('monday');\n$date = DateTime::instance('tomorrow');\n\n// Create from values\n$date = DateTime::createFromDate(2022, 12, 31);\n$date = DateTime::createFromDateAndTime(2022, 12, 31, 12, 30, 12);\n$date = DateTime::createFromDateAndTime(2022, 12, 31, 12, 30, 12, 6405);\n\n// Create from custom format\n$date = DateTime::createFromFormat('Y-m-d H:i:s', '2022-01-01 10:30:02');\n$date = DateTime::createFromFormat('Y-m-d H:i:s', '2022-01-01 10:30:02', DateTimeZone::LondonEurope());\n\n// Create an instance from another instance\n$date = DateTime::createFromInstance(new \\DateTime());\n$date = DateTime::createFromInstance(new \\Carbon());\n$date = DateTime::createFromInstance(new DateTime());\n\n// Quick shorthand creators\n$date = DateTime::now();       // 2020-01-02 12:30:00\n$date = DateTime::today();     // 2020-01-02 00:00:00\n$date = DateTime::yesterday(); // 2020-01-01 00:00:00\n$date = DateTime::tomorrow();  // 2020-01-03 00:00:00\n\n// Create from timestamps or micro-time\n$date = DateTime::createFromTimestamp(time());\n$date = DateTime::createFromTimestamp(microtime(true));\n$date = DateTime::createFromTimestamp(microtime());\n$date = DateTime::createFromTimestamp(time(), DateTimeZone::LondonEurope());\n$date = DateTime::createFromTimestamp(microtime(true), DateTimeZone::LondonEurope());\n$date = DateTime::createFromTimestamp(microtime(), DateTimeZone::LondonEurope());\n```\n\n#### Reading DateTime instances\n\n```php\nuse Kusabi\\Date\\DateTime;\nuse Kusabi\\Date\\DateTimeZone;\n\n$date = DateTime::createFromFormat('Y-m-d H:i:s', '2030-12-25 07:30:00');\n\n// Reading units\n$date-\u003egetDayOfMonth();      // 25\n$date-\u003egetDayOfWeek();       // 1\n$date-\u003egetDayName();         // Monday\n$date-\u003egetDayShortName();    // Mon\n$date-\u003egetDaysInMonth();     // 31\n$date-\u003egetDaysLeftInMonth(); // 6\n$date-\u003egetMonth();           // 1\n$date-\u003egetMonthName();       // January\n$date-\u003egetMonthShortName();  // Jan\n$date-\u003egetYear();            // 2030\n$date-\u003egetDayOfYear();       // 358\n$date-\u003egetHours();           // 7\n$date-\u003egetMinutes();         // 30\n$date-\u003egetSeconds();         // 0\n$date-\u003egetMicroseconds();    // 0\n\n// Convert to native datetime\n$native = $date-\u003etoNative();\n\n// Get the timezone\n$date-\u003egetTimezone();\n$date-\u003egetTimezone()-\u003egetCountryCode(); // CZ\n\n// Common checks\n$date-\u003eisWeekday(); // true\n$date-\u003eisWeekend(); // false\n```\n\n#### Modifying DateTime instances\n```php\nuse Kusabi\\Date\\DateTime;\nuse Kusabi\\Date\\DateTimeZone;\n\n// Set time zone\n$date = new DateTime();\n$date-\u003esetTimezone(DateTimeZone::LondonEurope());\n$date-\u003esetTimezone(DateTimeZone::AbidjanAfrica(), true); // Lock the date time string, by translating the timestamp by the offset\n\n// Update from format\n$date = new DateTime();\n$date-\u003esetFromFormat('Y-m-d H:i:s', '2030-12-25 07:30:00');\n\n// Moving to limits\n$date = new DateTime();\n$date-\u003eendOfDay(); // 2020-01-02 23:59:59\n$date-\u003eendOfMonth(); // 2020-01-31 00:00:00\n$date-\u003eendOfMonth()-\u003eendOfDay(); // 2020-01-31 23:59:59\n$date-\u003estartOfDay(); // 2020-01-02 00:00:00\n\n// Setting units\n$date-\u003esetDay(25);\n$date-\u003esetMonth(12);\n$date-\u003esetYear(12);\n$date-\u003esetDate(2020, 12, 25);\n$date-\u003esetHours(7);\n$date-\u003esetMinutes(30);\n$date-\u003esetSeconds(30);\n$date-\u003esetMicroseconds(500);\n$date-\u003esetTime(7, 30, 0);\n$date-\u003esetTime(7, 30, 0, 500);\n$date-\u003esetDay(25)-\u003esetMonth(12)-\u003esetYear(2020)-\u003esetTime(7, 30, 0);\n$date-\u003esetDate(2020, 12, 25)-\u003esetTime(7, 30, 0);\n$date-\u003esetDateAndTime(2020, 12, 25, 7, 30, 0);\n$date-\u003esetDateAndTime(2020, 12, 25, 7, 30, 0, 34623);\n$date-\u003esetFromFormat('Y-m-d H:i:s.u', '2023-12-31 12:30:00.555666');\n\n// Adding units\n$date-\u003eaddMicrosecond();\n$date-\u003eaddMicroseconds(10);\n$date-\u003eaddSecond();\n$date-\u003eaddSeconds(10);\n$date-\u003eaddMinute();\n$date-\u003eaddMinutes(10);\n$date-\u003eaddHour();\n$date-\u003eaddHours(10);\n$date-\u003eaddDay();\n$date-\u003eaddDays(10);\n$date-\u003eaddMonth();\n$date-\u003eaddMonths(10);\n$date-\u003eaddYear();\n$date-\u003eaddYears(10);\n\n// Subtracting units\n$date-\u003esubMicrosecond();\n$date-\u003esubMicroseconds(10);\n$date-\u003esubSecond();\n$date-\u003esubSeconds(10);\n$date-\u003esubMinute();\n$date-\u003esubMinutes(10);\n$date-\u003esubHour();\n$date-\u003esubHours(10);\n$date-\u003esubDay();\n$date-\u003esubDays(10);\n$date-\u003esubMonth();\n$date-\u003esubMonths(10);\n$date-\u003esubYear();\n$date-\u003esubYears(10);\n```\n\n#### Converting DateTime instances\n```php\nuse Kusabi\\Date\\DateTime;\nuse Kusabi\\Date\\DateTimeZone;\n\n// Cast to string\n$date = DateTime::createFromFormat('Y-m-d H:i:s', '2020-06-18 14:11:22', DateTimeZone::BrokenHillAustralia());\necho $datetime-\u003etoString(); // 2020-06-18T14:11:22+09:30\necho (string) $datetime; // 2020-06-18T14:11:22+09:30\n```\n\n## Using the DateInterval class\n\nThe `DateInterval` class has been extended to add new ways of creating, reading, modifying, optimising, cloning and converting the instance.\n\n#### Creating DateInterval instances\n```php\nuse Kusabi\\Date\\DateInterval;\n\n// Create a basic instance\n$interval = new DateInterval(); // Defaults to PT0S (0 second interval)\n$interval = new DateInterval('P1YT10S');\n\n// Create an instance with a chain-able static method\n$interval = DateInterval::instance(); // Defaults to PT0S (0 second interval)\n$interval = DateInterval::instance('P1YT10S');\n\n// Create an instance from another instance\n$interval = DateInterval::createFromInstance(new \\DateInterval('P1YT10S'));\n$interval = DateInterval::createFromInstance(new \\Carbon\\CarbonInterval('P1YT10S'));\n$interval = DateInterval::createFromInstance(new DateInterval('P1YT10S'));\n\n// Create from values\n$interval = DateInterval::createFromValues(1, 2, 3, 4, 5, 6); // years, months, days, hours, minutes, seconds\n\n// Quickly create common intervals\n$interval = DateInterval::second();\n$interval = DateInterval::seconds(15);\n$interval = DateInterval::minute();\n$interval = DateInterval::minutes(30);\n$interval = DateInterval::hour();\n$interval = DateInterval::hours(8);\n$interval = DateInterval::day();\n$interval = DateInterval::days(7);\n$interval = DateInterval::week();\n$interval = DateInterval::weeks(4);\n$interval = DateInterval::month();\n$interval = DateInterval::months(6);\n$interval = DateInterval::year();\n$interval = DateInterval::years(100);\n```\n\n#### Reading DateInterval instances\n```php\nuse Kusabi\\Date\\DateInterval;\n\n// Get the spec from the instance\n$interval = DateInterval::year()-\u003egetSpec(); // P1Y\n```\n\n#### Modifying DateInterval instances\n```php\nuse Kusabi\\Date\\DateInterval;\n\n// Optimise the interval into it's larger components\n$interval = DateInterval::seconds(125)-\u003eoptimise(); // Becomes PT2M5S (2 minutes and 5 seconds)\n\n// Optimise into a cloned instance\n$interval = DateInterval::seconds(125);\n$optimisedClone = $interval-\u003eoptimised();\n\n// Clone the instance\n$interval = new DateInterval();\n$cloned = $interval-\u003ecloned();\n\n// Modifying the instance\n$interval = new DateInterval();\n$interval-\u003eaddSecond();\n$interval-\u003eaddSeconds(10);\n$interval-\u003eaddMinute();\n$interval-\u003eaddMinutes(10);\n$interval-\u003eaddHour();\n$interval-\u003eaddHours(10);\n$interval-\u003eaddDay();\n$interval-\u003eaddDays(10);\n$interval-\u003eaddMonth();\n$interval-\u003eaddMonths(10);\n$interval-\u003eaddYear();\n$interval-\u003eaddYears(10);\n$interval-\u003eaddInterval(new \\DateInterval('P1YT10S'));\n$interval-\u003eaddInterval(DateInterval::month());\n\n$interval-\u003esubSecond();\n$interval-\u003esubSeconds(10);\n$interval-\u003esubMinute();\n$interval-\u003esubMinutes(10);\n$interval-\u003esubHour();\n$interval-\u003esubHours(10);\n$interval-\u003esubDay();\n$interval-\u003esubDays(10);\n$interval-\u003esubMonth();\n$interval-\u003esubMonths(10);\n$interval-\u003esubYear();\n$interval-\u003esubYears(10);\n$interval-\u003esubInterval(new \\DateInterval('P1YT10S'));\n$interval-\u003esubInterval(DateInterval::month());\n```\n\n## Using the DatePeriod class\n\nThe `DatePeriod` class has been extended to add new ways of creating, reading, modifying, optimising, cloning and converting the instance.\n\n#### Creating DatePeriod instances\n```php\nuse Kusabi\\Date\\DateTime;\nuse Kusabi\\Date\\DatePeriod;\n\n// Create a basic instance\n$period = new DatePeriod(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5));\n\n// Create an instance with a chain-able static method\n$period = DatePeriod::instance(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5));\n\n// Create an instance from another instance\n$period = DatePeriod::createFromInstance(new \\DatePeriod('P1YT10S'));\n$period = DatePeriod::createFromInstance(new \\Carbon\\CarbonPeriod('P1YT10S'));\n$period = DatePeriod::createFromInstance(new DatePeriod('P1YT10S'));\n\n```\n\n#### Reading DateInterval instances\n```php\nuse Kusabi\\Date\\DatePeriod;\nuse Kusabi\\Date\\DateInterval;\n\n// Get the interval\n$period = new DatePeriod(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5))\n$interval = $period-\u003egetDateInterval()-\u003egetSpec(); // P1D\n\n// Get the start datetime\n$period = new DatePeriod(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5))\n$datetime = $period-\u003egetStartDate()-\u003eformat('Y-m-d'); // '2022-01-01'\n\n// Get the end datetime\n$period = new DatePeriod(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5))\n$datetime = $period-\u003egetEndDate()-\u003eformat('Y-m-d'); // '2022-01-06'\n\n// Get the all the date times as an array\n$period = new DatePeriod(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5))\n$dates = $period-\u003egetDateTimes();\n\n// Converting to a string\n$period = new DatePeriod(DateTime::today(), DateInterval::day(), DateTime::today()-\u003eaddDays(5))\n$period-\u003etoString(); // every +1 day between 2022-01-01T00:00:00+00:00 and 2022-01-06T00:00:00+00:00\n(string) $period; // every +1 day between 2022-01-01T00:00:00+00:00 and 2022-01-06T00:00:00+00:00\n```\n\n\n## DateTimeZone\n\n```php\nuse Kusabi\\Date\\DateTimeZone;\n\n// Create a basic instance\n$timezone = new DateTimeZone();\n$timezone = new DateTimeZone('Asia/Yakutsk');\n\n// Create an instance with a chain-able static method\n$timezone = DateTimeZone::instance();\n$timezone = DateTimeZone::instance('Asia/Yakutsk');\n\n// Create an instance from another instance\n$legacy = new \\DateTimeZone('UTC');\n$timezone = DateTimeZone::createFromInstance($legacy);\n\n// Quickly create timezones for a particular area\n$timezone = DateTimeZone::PerthAustralia();\n$timezone = DateTimeZone::PhoenixAmerica();\n$timezone = DateTimeZone::TokyoAsia();\n\n// Quick access to timezone data\n$lat = $timezone-\u003egetLatitude();\n$lon = $timezone-\u003egetLongitude();\n$code = $timezone-\u003egetCountryCode();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkusabi%2Fdatetime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkusabi%2Fdatetime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkusabi%2Fdatetime/lists"}