处理独立于日期的时间操作的类。

v1.1.0 2016-10-05 11:48 UTC

This package is not auto-updated.

Last update: 2024-09-20 21:50:13 UTC


README

已迁移至 https://gitlab.com/vascowhite/Time

时间

Build Status

简介

这是一个处理时间的类。

这个时间数据类型表示一个时间段。它以 'H:i:s'(datetime表示的左截断)格式表示。这是在不知道日期、时区或DST的秒表上测量的经过的时间。

PHP的本地\DatePeriod非常适合表示任何长度的时期,但是它不适合操作时期或与它们进行计算。因此,这个类应运而生。目前它的范围仅限于小时、分钟和秒,这允许在没有担心DST等的情况下进行准确的操作,而DateTime类已经很好地解决了这些问题。

这个类可以添加、减去、平均、求和和比较时间。它还可以将\DateInterval对象转换为TimeValue对象,并将TimeValue对象转换为\DateInterval对象。

安装

使用composer安装,在composer.json中添加以下内容:

"require": {
    "vascowhite/time": "1.1.0"
}

其他安装方法可能可行,但不被支持。

需求

需要PHP >= 5.5.0

TimeValue

这是一个表示时间数据类型的不可变类。它对日期一无所知,如果您需要与日期关联的时间,那么您需要查找PHP的\DateTime类。

有各种方法可用于操作和比较TimeValue对象。

TimeValue对象实现了__toString()魔法方法,因此可以打印等。

TimeValue::__construct()

签名:

TimeValue __construct(String $time, String $format = 'H:i:s')

参数

$time是一个表示时间段字符串。例如,一小时、十六分钟和三十秒将表示为:'01:16:30'。

$format可选格式字符串,默认为'H:i:s'。可用格式为'H:i:s'、'H'、'i'或's'。

以下是有效格式的示例:

new TimeValue('12:15:20'); // 12 hours 15 minutes 20 seconds
new TimeValue('12', 'H'); // 12 hours 0 minutes 0 seconds
new TimeValue('12:15', 'H:i'); // 12 hours 15 minutes
new TimeValue('12:15', 'i:s'); // 12 minutes 15 seconds
new TimeValue('20', 's'); // 20 seconds.

尽管指定了格式,但没有任何字段限制为两位数。以下也是有效的:

new TimeValue('120:150:200'); // 120 hours 150 minutes 200 seconds Will output '122:33:20'
new TimeValue('00:00:36000'); // 36000 seconds. Will output '10:00:00'

返回返回一个TimeValue对象。

TimeValue::getSeconds()

签名

Int getSeconds();

参数

无。

返回返回一个表示TimeValue所跨越秒数的整数。

示例

$time = new TimeValue('00:10:10');
echo $time->getSeconds; // Output 610

TimeValue::getTime()

签名

String getTime()

参数

无。

返回

返回一个表示时间的字符串,格式为'H:i:s'。'H'部分将扩展到所需位数来表示小时。

示例

$time = new TimeValue('00:00:36000');
echo $time->getTime(); // Output "10:00:00"

TimeValue::add()

签名

TimeValue add(TimeValue)

参数

要添加的TimeValue

返回

返回一个设置为适当秒数的TimeValue对象。

示例

$time = new TimeValue('01:00:00');
echo $time->add(new TimeValue('30', 'i'); // Output "01:30:00"

TimeValue::sub()

签名

TimeValue sub(TimeValue)

参数

要减去的TimeValue

返回

返回一个设置为适当秒数的TimeValue对象。

示例

$time = new TimeValue('01:00:00');
echo $time->sub(new TimeValue('00:30'); // Output "00:30:00"

TimeValue::average()

签名

TimeValue average(TimeValues[])

参数

一个TimeValue对象数组。

返回

返回一个设置为所提供数组中TimeValue对象平均秒数的TimeValue对象。

示例

$timeValue1 = new TimeValue('00:20:00'); //1200 seconds
$timeValue2 = new TimeValue('00:10:00'); //600 seconds
$timeValue3 = new TimeValue('00:30:00'); //1800 seconds
$average = TimeValue::average([$timeValue1, $timeValue2, $timeValue3]);
echo $average->getSeconds(); //Output = 1200

TimeValue::sum()

签名

TimeValue sum(TimeValues[])

参数

一个TimeValue对象数组。

返回

返回一个设置为所提供数组中TimeValue对象总和的TimeValue对象。

示例

$timeValue1 = new TimeValue('00:20:00'); //1200 seconds
$timeValue2 = new TimeValue('00:10:00'); //600 seconds
$timeValue3 = new TimeValue('00:30:00'); //1800 seconds
$sum = TimeValue::sum([$timeValue1, $timeValue2, $timeValue3]);
echo $sum->getSeconds(); //Output = 3600

TimeValue::createFromDateInterval()

签名

TimeValue createFromDateInterval(\DateInterval, bool)

参数

一个\DateInterval对象。一个布尔值。如果为真,返回的\DateInterval对象将表示负值。默认为false。

返回

返回一个设置为表示\DateInterval对象所表示秒数的TimeValue对象。

示例

$interval = new \DateInterval('P1Y1M6DT14H12M6S');
$timeValue = TimeValue::createFromDateInterval($interval); //34783926 seconds

TimeValue::toDateInterval()

签名

TimeValue||Bool toDateInterval()

参数

无。

返回

返回一个所有字段设置为仿佛由 \DateTime::diff() 创建的 \DateInterval 对象。如果转换失败,则返回 false

示例

$timeValue = new TimeValue('34783926', 's');
var_dump($timeValue);
/*
Output
object(DateInterval)[2]
   public 'y' => int 1
   public 'm' => int 1
   public 'd' => int 6
   public 'h' => int 14
   public 'i' => int 12
   public 's' => int 6
   public 'weekday' => int 0
   public 'weekday_behavior' => int 0
   public 'first_last_day_of' => int 0
   public 'invert' => int 0
   public 'days' => int 402
   public 'special_type' => int 0
   public 'special_amount' => int 0
   public 'have_weekday_relative' => int 0
   public 'have_special_relative' => int 0
*/

TimeValue::format()

签名

string format(string)

参数

表示所需格式的字符串。使用与 \DateInterval::format() 相同的格式化方式。