add Time support class #189

This commit is contained in:
Nabeel Shahzad 2018-02-10 21:36:13 -06:00
parent a8e06c6cc6
commit cb02a7c15e
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace App\Support\Units;
/**
* Class Time
* @package App\Support\Units
*/
class Time
{
public $hours,
$minutes;
/**
* Pass just minutes to figure out how many hours
* Or both hours and minutes
* @param $minutes
* @param $hours
*/
public function __construct($minutes, $hours=null)
{
if(!empty($hours)) {
$this->hours = $hours;
} else {
$this->hours = floor($minutes / 60);
}
$this->minutes = $minutes % 60;
}
/**
* Get the total number minutes, adding up the hours
* @return float|int
*/
public function getMinutes()
{
return ($this->hours * 60) + $this->minutes;
}
/**
* Alias to getMinutes()
* @alias getMinutes()
* @return float|int
*/
public function asInt()
{
return $this->getTotalMinutes();
}
/**
* Return a time string
* @return string
*/
public function __toString()
{
return $this->hours . 'h ' . $this->minutes . 'm';
}
}

View File

@ -9,6 +9,9 @@ class UtilsTest extends TestCase
public function testSecondsToTimeParts()
{
$t = new \PhpUnitsOfMeasure\PhysicalQuantity\Time(65, 'm');
echo $t->toUnit('hours');
$t = Utils::secondsToTimeParts(3600);
$this->assertEquals(['h' => 1, 'm' => 0, 's' => 0], $t);