arokettu/datetime-rounding

DateTime rounding helper

1.0.0 2023-11-15 18:35 UTC

This package is auto-updated.

Last update: 2024-02-16 15:24:14 UTC


README

Packagist PHP License Gitlab pipeline status Codecov

A tool to truncate a DateTime instance to a specific time unit.

Installation

composer require arokettu/datetime-rounding

Example

<?php

use Arokettu\DateTime\DateTimeTruncate;
use Carbon\CarbonImmutable;

// to hours
$dateTime = new DateTimeImmutable('2012-03-04T05:06:07.890123Z');
echo DateTimeTruncate::toHours($dateTime)->format('c'); // 2012-03-04T05:00:00+00:00

// truncating to dates uses the DT timezone
$dateTime = new DateTimeImmutable('2012-03-04T05:06:07.890123 Europe/Stockholm');
echo DateTimeTruncate::toMonths($dateTime)->format('c'); // 2012-03-01T00:00:00+01:00

// using mutable will change the object
$dateTime = new DateTime('2012-03-04T05:06:07.890123Z');
DateTimeTruncate::toMinutes($dateTime);
echo $dateTime->format('c'); // 2012-03-04T05:06:00+00:00

// the tool tries to preserve the extended objects as well
$dateTime = new CarbonImmutable('2012-03-04T05:06:07.890123Z');
echo get_class(DateTimeTruncate::toMonths($dateTime)); // Carbon\CarbonImmutable

Documentation

Read full documentation here: https://sandfox.dev/php/datetime-rounding.html

Support

Please file issues on our main repo at GitLab: https://gitlab.com/sandfox/datetime-rounding/-/issues

Feel free to ask any questions in our room on Gitter: https://gitter.im/arokettu/community

License

The library is available as open source under the terms of the MIT License.