arokettu/datetime-rounding

DateTime舍入辅助工具

1.0.0 2023-11-15 18:35 UTC

This package is auto-updated.

Last update: 2024-08-25 17:39:33 UTC


README

Packagist PHP License Gitlab pipeline status Codecov

一个将DateTime实例截断到特定时间单位的工具。

安装

composer require arokettu/datetime-rounding

示例

<?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

文档

请在此处阅读完整文档:https://sandfox.dev/php/datetime-rounding.html

支持

请在我们GitLab的仓库中提交问题: https://gitlab.com/sandfox/datetime-rounding/-/issues

在Gitter的聊天室中随时提问: https://gitter.im/arokettu/community

许可证

该库以开源形式提供,遵循MIT许可证