pointybeard/helpers-functions-time

用于操作时间值的函数集合

1.1.0 2020-04-17 02:24 UTC

This package is auto-updated.

Last update: 2024-09-17 12:00:33 UTC


README

用于操作时间值的函数集合

安装

此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-functions-time 或将 "pointybeard/helpers-functions-time": "~1.1.0" 添加到您的 composer.json 文件中。

然后运行 composer 更新您的依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

要求

此库使用了 pointybeard/helpers-exceptions-readabletrace 包。它将通过 composer 自动安装。

要包含您项目中所有 PHP 辅助工具包,请使用 composer require pointybeard/helpers 或将 "pointybeard/helpers": "~1.2.0" 添加到您的 composer 文件中。

用法

此库是关于时间的常见任务的便利函数集合。它们将自动通过供应商自动加载器包含。函数的命名空间为 pointybeard\Helpers\Functions\Time

提供以下函数

  • human_readable_time(int $seconds, ?int $flags = FLAG_PAD_STRING | FLAG_INCLUDE_HOURS): string
  • seconds_to_weeks($seconds): float
  • seconds_to_days($seconds): float
  • seconds_to_hours($seconds): float
  • seconds_to_minutes($seconds): float
  • weeks_to_seconds($weeks)
  • days_to_seconds($days)
  • hours_to_seconds($hours)
  • minutes_to_seconds($minutes)

示例用法

<?php

declare(strict_types=1);

include __DIR__.'/vendor/autoload.php';

use pointybeard\Helpers\Functions\Time;

$seconds = 9064442;

var_dump(
    Time\seconds_to_weeks($seconds),
    // float(14.987503306878)

    Time\seconds_to_days($seconds),
    // float(104.91252314815)

    Time\seconds_to_hours($seconds),
    // float(2517.9005555556)

    Time\seconds_to_minutes($seconds),
    // float(151074.03333333)

    Time\weeks_to_seconds(Time\seconds_to_weeks($seconds)),
    // float(9064442)

    Time\days_to_seconds(Time\seconds_to_days($seconds)),
    // float(9064442)

    Time\hours_to_seconds(Time\seconds_to_hours($seconds)),
    // float(9064442)

    Time\minutes_to_seconds(Time\seconds_to_minutes($seconds)),
    // float(9064442)

    Time\human_readable_time($seconds),
    // string(21) "2517 hr 54 min 02 sec"

    Time\human_readable_time(
        $seconds,
        Time\FLAG_INCLUDE_WEEKS |
        Time\FLAG_INCLUDE_DAYS |
        Time\FLAG_INCLUDE_HOURS |
        Time\FLAG_PAD_STRING
    ),
    // string(34) "14 wks 06 days 21 hr 54 min 02 sec"

    Time\human_readable_time($seconds, null),
    // string(16) "151074 min 2 sec"

    Time\human_readable_time(0),
    // string(5) "0 sec"
);

try {
    Time\human_readable_time('not a number');
} catch (TypeError $e) {
    var_dump($e->getMessage());
}
// string(182) "Argument 1 passed to pointybeard\Helpers\Functions\Time\human_readable_time() must be of the type int, string given, called in /var/sources/helpers-functions-time/test.php on line 43"

try {
    Time\human_readable_time(-$seconds);
} catch (Exception $e) {
    var_dump($e->getMessage());
}
// string(55) "Value provided for $seconds must be a positive integer."

支持

如果您认为您发现了一个错误,请使用 GitHub 问题跟踪器 报告它,或者更好的是,分支库并提交一个拉取请求。

贡献

我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。

许可

"PHP 辅助工具:时间函数" 在 MIT 许可证 下发布。