{"id":15026357,"url":"https://github.com/prokki/ext-datetime","last_synced_at":"2026-03-16T16:03:33.238Z","repository":{"id":62530909,"uuid":"199153812","full_name":"prokki/ext-datetime","owner":"prokki","description":"Extends the common php DateTime objects with additional helpful methods.","archived":false,"fork":false,"pushed_at":"2019-07-31T16:41:16.000Z","size":18,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-19T20:23:30.446Z","etag":null,"topics":["datetime","php73"],"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/prokki.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-27T11:05:13.000Z","updated_at":"2019-08-03T20:17:37.000Z","dependencies_parsed_at":"2022-11-02T15:31:55.101Z","dependency_job_id":null,"html_url":"https://github.com/prokki/ext-datetime","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prokki%2Fext-datetime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prokki%2Fext-datetime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prokki%2Fext-datetime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prokki%2Fext-datetime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prokki","download_url":"https://codeload.github.com/prokki/ext-datetime/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243330323,"owners_count":20274039,"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":["datetime","php73"],"created_at":"2024-09-24T20:04:20.430Z","updated_at":"2025-12-28T16:29:52.810Z","avatar_url":"https://github.com/prokki.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ext-DateTime\n\n[![LICENSE](https://img.shields.io/badge/release-0%2E0%2E0-blue.svg?style=flat)](hhttps://github.com/prokki/ext-datetime/releases/tag/0.0.0)\n[![Packagist](https://img.shields.io/badge/Packagist-0%2E0%2E0-blue.svg?style=flat)](https://packagist.org/packages/prokki/ext-datetime)\n[![LICENSE](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)](LICENSE)\n[![PHP v7.3](https://img.shields.io/badge/PHP-%E2%89%A57%2E3-0044aa.svg)](https://www.php.net/manual/en/migration73.new-features.php)\n[![codecov](https://codecov.io/gh/prokki/ext-datetime/branch/master/graph/badge.svg)](https://codecov.io/gh/prokki/ext-datetime)\n[![Build Status](https://travis-ci.org/prokki/ext-datetime.svg?branch=master)](https://travis-ci.org/prokki/ext-datetime)\n\nExtends the native datetime objects ([\\DateTime](https://www.php.net/manual/en/class.datetime.php) and [\\DateTimeImmutable](https://www.php.net/manual/en/class.datetimeimmutable.php)) with a lot of additional helpful methods.\n\nMost of the new methods are **short cuts of already existing functionality**.\nBut these methods will help you to save time and code if you need to handle\na many date- and time-operations.\n\nAll new methods support [*method chaining*](https://stackoverflow.com/questions/3724112/php-method-chaining). \n\n## Table of Contents\n\n* [Requirements](#requirements)\n* [Integration](#integration)\n* [Usage](#usage)\n  * [Static Initialization](#static-initialization)\n  * [Cloning](#cloning)\n  * [Manipulating](#manipulating)\n  * [Instant Setter Methods](#instant-setter-methods)\n\n\n\n## Requirements\n\nThe usage of [**PHP v7.3**](https://www.php.net/manual/en/migration73.new-features.php) is obligatory.\n\n\n\n## Integration\n\nPlease install via [composer](https://getcomposer.org/).\n\n```bash\ncomposer require prokki/ext-datetime \"^0.0\"\n```\n\n\n\n## Usage\n\nThe classes and instances of the classes [ExtDateTime/DateTime](src/DateTime.php) and [ExtDateTime/DateTimeImmutable](src/DateTimeImmutable.php) can be used\nexactly like native datetime objects/classes.\n\nExample\n\n```php\nuse ExtDateTime\\DateTime;\nuse ExtDateTime\\DateTimeImmutable;\n\n// create a datetime object\n$dateTime = new DateTime(\"now\");\n\n// create an immutable datetime object\n$dateTimeImmutable = new DateTimeImmutable(\"now\");\n```\n\n\n### Static Initialization\n\nSimilar to the native objects there are several others ways to create a datetime object.\n\nAll static methods can be used to implement [*method chaining*](https://stackoverflow.com/questions/3724112/php-method-chaining). \n\n#### create()\n\nUse the static constructor `create()` to use chaining immediately.\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object\n// and use chaining immediately\n$dateTime = DateTime::create(\"now\")\n            -\u003eaddHours(5)\n            -\u003eaddDays(5)\n            ;\n```\n\n#### current()\n\nThe static constructor `current()` returns a datetime object with the current date/time.\nBut in opposite to `DateTime::create(\"now\")` this method returns the object **with additional microtime**. \n\n```php\nuse ExtDateTime\\DateTime;\n\n$currentMicroseconds = DateTime::current()-\u003eformat(\"u\");         // output example 654321\n$noMicroseconds      = DateTime::create(\"now\")-\u003eformat(\"u\");     // output always 000000\n```\n\n#### createFromObject()\n\nCreates a new object from any datetime object implementing the [DateTimeInterface](https://www.php.net/manual/en/class.datetimeinterface.php).\n\n```php\nuse ExtDateTime\\DateTimeImmutable;\n\n// create an immutable datetime object from a native non-immutable object\n$datetime = DateTimeImmutable::createFromObject(new \\DateTime());\n```\n\n\n### Cloning\n\nTwo new methods add the availability to use chaining directly after cloning an object.\n\n#### duplicate()\n\nThis method is just a wrapper function for `clone`:\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object\n$datetime = DateTime::current();\n\n// clone the datetime object and proceed like usual with chaining\n$clone = $datetime-\u003eduplicate()\n         -\u003eaddHours(5)\n         -\u003eaddDays(5)\n         ;\n```\n\n#### toImmutable() / toMutable()\n\nInstead of using static constructors ([DateTime::createFromImmutable](https://www.php.net/manual/en/datetime.createfromimmutable.php) or [DateTimeImmutable::createFromMutable](https://www.php.net/manual/en/datetimeimmutable.createfrommutable.php)) you can also use these new non-static methods.\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object\n$datetime = DateTime::current();\n\n$clonedImmutable = $datetime-\u003etoImmutable();       // only available in class DateTime\n\n$clonedMutable   = $clonedImmutable-\u003etoMutable();  // only available in class DateTimeImmutable\n```\n\n\n\n### Manipulating\n\nMost of the new methods are short cuts to avoid re-initialization of necessary parameter in your code.\n\n#### addHours() / subHours()\n\nAdds or subtracts hours of a datetime object.\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and add 10 hours\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003eaddHours(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-30 22:35:17\"\n\n// create a datetime object and subtracts 10 hours\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003esubHours(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-30 02:35:17\"\n```\n\n#### addDays() / subHours()\n\nAdds or subtracts days of a datetime object.\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and add 10 days\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003eaddDays(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-08-09 22:35:17\"\n\n// create a datetime object and subtracts 10 days\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003esubDays(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-20 02:35:17\"\n```\n\n#### addMonth() / subMonth()\n\nAdds or subtracts months of a datetime object.\n\n**Attention**: This methods behaves differently than the suspected background function\nof adding days. If the current date is the last day of the month (31/30/29/28) and the target month\nhas less days then the current month, the day will be set to the last day of the target month.\n\nExample: The datetime object is\n\n\u003e *2017-01-30 17:00:00*\n\nand you want to add _1 month_, the result will be\n\n\u003e *2017-02-28 17:00:00*\n\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and add 10 months\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003eaddMonth(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-08-09 22:35:17\"\n\n// create a datetime object and subtracts 10 months\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003esubMonth(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-20 02:35:17\"\n```\n\n\n\n### Instant Setter Methods\n\n#### toEndOfDay()\n\nSets the time to the end of the day (*23:59:59*).\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and sets the time to the end of the day\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003etoEndOfDay(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-30 23:59:59\"\n```\n\n#### toNoon()\n\nSets the time to noon (*12:00:00*).\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and sets the time to noon\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003etoNoon(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-30 12:00:00\"\n```\n\n#### toStartOfDay()\n\nSets the time to the start of the day  (*00:00:00*).\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and sets the time the start of the day\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003etoStartOfDay(10)\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-30 00:00:00\"\n```\n\n#### toStartOfMonth()\n\nSets the date to the first day of the month and additionally the time to the start of the day (*00:00:00*).\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and sets the date to the first day of the month\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003etoStartOfMonth()\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-01 00:00:00\"\n```\n\n#### toEndOfMonth()\n\nSets the date to the last day of the month and additionally the time to the end of the day (*23:59:59*).\n\n```php\nuse ExtDateTime\\DateTime;\n\n// create a datetime object and sets the date to the last day of the month\n$datetimeFuture = DateTime::create(\"2020-07-30 12:35:17\")\n                  -\u003etoEndOfMonth()\n                  -\u003eformat(\"Y-m-d h:i:s\");                   // \"2020-07-31 23:59:59\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprokki%2Fext-datetime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprokki%2Fext-datetime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprokki%2Fext-datetime/lists"}