https://github.com/ryanwinchester/carbonize
Carbon helper
https://github.com/ryanwinchester/carbonize
carbon datetime php
Last synced: about 1 year ago
JSON representation
Carbon helper
- Host: GitHub
- URL: https://github.com/ryanwinchester/carbonize
- Owner: ryanwinchester
- License: mit
- Created: 2017-10-14T11:19:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-15T06:32:03.000Z (over 8 years ago)
- Last Synced: 2025-04-08T12:51:48.381Z (over 1 year ago)
- Topics: carbon, datetime, php
- Language: PHP
- Size: 45.9 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Carbonize
[](https://packagist.org/packages/ryanwinchester/carbonize)
[](https://travis-ci.org/ryanwinchester/carbonize)
[](https://codecov.io/gh/ryanwinchester/carbonize)
[](https://codeclimate.com/github/ryanwinchester/carbonize/maintainability)
[Carbon](https://github.com/briannesbitt/Carbon) helper for creating new instances of Carbon from other Carbon objects,
DateTime|Immutable objects, date strings, timestamps, or null (for `now`).
Sometimes we have to work on projects that already exist or are underway, that are full of magic and inconsistent date formats.
Is this going to be a timestamp? a date string? a Carbon instance? Does it even matter as long as it's a datetime of some sort? ¯\\\_(ツ)\_/¯
```php
public function doMyThing($datetime)
{
// Whatever it was, it is _now_ a NEW carbon instance
$datetime = carbonize($datetime);
// do stuff with your carbon instance
}
```
You can use it as either `carbonize()`, `carbon()`, or `Carbonize\Carbonize::toCarbon()`.
## Install
```
composer require ryanwinchester/carbonize
```
This does not install carbon as a dependency, so add it yourself.
**Important Note:** Please use Carbon 1.21 (not 1.22) until timezones are fixed. (https://github.com/briannesbitt/Carbon/issues/863)
## What it does
```php
carbonize() == Carbon::now("UTC");
$carbon = new Carbon();
carbonize($carbon) == $carbon->copy();
$dt = new DateTime();
carbonize($dt) == Carbon::instance($dt);
$dtImmutable = new DateTimeImmutable();
carbonize($dtImmutable) == Carbon::instance(new DateTime($dtImmutable->format(DateTime::ATOM)));
carbonize(1507957785) == Carbon::createFromTimestamp(1507957785, "UTC");
carbonize("1507957785") == Carbon::createFromTimestamp(1507957785, "UTC");
carbonize("2017-01-01 12:04:01") == Carbon::parse("2017-01-01 12:04:01", "UTC");
carbonize("3 months ago") == Carbon::parse("3 months ago", "UTC");
```
## License
MIT
## Credits
- **Carbon** (https://github.com/briannesbitt/Carbon)
## Notes
Although Carbon is decent, use [Chronos](https://github.com/cakephp/chronos) if you can. (https://github.com/cakephp/chronos)
<3 Chronos, Immutability FTW ᕙ(⇀‸↼‶)ᕗ