2018-02-11 11:36:13 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support\Units;
|
2018-02-21 12:33:09 +08:00
|
|
|
|
2018-02-11 12:04:30 +08:00
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
2018-02-11 11:36:13 +08:00
|
|
|
|
2018-02-11 12:04:30 +08:00
|
|
|
class Time implements Arrayable
|
2018-02-11 11:36:13 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
public $hours;
|
|
|
|
public $minutes;
|
2018-02-11 11:36:13 +08:00
|
|
|
|
2018-02-20 03:33:26 +08:00
|
|
|
/**
|
|
|
|
* @param $minutes
|
|
|
|
* @param $hours
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-20 03:33:26 +08:00
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function init($minutes, $hours)
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
return new self($minutes, $hours);
|
2018-02-20 03:33:26 +08:00
|
|
|
}
|
|
|
|
|
2018-02-11 11:36:13 +08:00
|
|
|
/**
|
|
|
|
* Pass just minutes to figure out how many hours
|
|
|
|
* Or both hours and minutes
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 11:36:13 +08:00
|
|
|
* @param $minutes
|
|
|
|
* @param $hours
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function __construct($minutes, $hours = null)
|
2018-02-11 11:36:13 +08:00
|
|
|
{
|
2018-02-12 10:38:56 +08:00
|
|
|
$minutes = (int) $minutes;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!empty($hours)) {
|
2018-02-12 10:38:56 +08:00
|
|
|
$this->hours = (int) $hours;
|
2018-02-11 11:36:13 +08:00
|
|
|
} else {
|
|
|
|
$this->hours = floor($minutes / 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->minutes = $minutes % 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total number minutes, adding up the hours
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 11:36:13 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public function getMinutes()
|
|
|
|
{
|
|
|
|
return ($this->hours * 60) + $this->minutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias to getMinutes()
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 11:36:13 +08:00
|
|
|
* @alias getMinutes()
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 11:36:13 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public function asInt()
|
|
|
|
{
|
2018-02-11 12:04:30 +08:00
|
|
|
return $this->getMinutes();
|
2018-02-11 11:36:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a time string
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 11:36:13 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
return $this->hours.'h '.$this->minutes.'m';
|
2018-02-11 11:36:13 +08:00
|
|
|
}
|
2018-02-11 12:04:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public function toObject()
|
|
|
|
{
|
|
|
|
return $this->getMinutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the instance as an array.
|
|
|
|
*/
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
return $this->getMinutes();
|
|
|
|
}
|
2020-02-12 01:32:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $minutes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function minutesToTimeParts($minutes): array
|
|
|
|
{
|
|
|
|
$hours = floor($minutes / 60);
|
|
|
|
$minutes %= 60;
|
|
|
|
|
|
|
|
return ['h' => $hours, 'm' => $minutes];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function minutesToTimeString($minutes): string
|
|
|
|
{
|
|
|
|
$hm = self::minutesToTimeParts($minutes);
|
|
|
|
return $hm['h'].'h '.$hm['m'].'m';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert seconds to an array of hours, minutes, seconds
|
|
|
|
*
|
|
|
|
* @param int $seconds
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*
|
|
|
|
* @return array['h', 'm', 's']
|
|
|
|
*/
|
|
|
|
public static function secondsToTimeParts($seconds): array
|
|
|
|
{
|
|
|
|
$dtF = new \DateTimeImmutable('@0', new \DateTimeZone('UTC'));
|
|
|
|
$dtT = new \DateTimeImmutable("@$seconds", new \DateTimeZone('UTC'));
|
|
|
|
|
|
|
|
$t = $dtF->diff($dtT);
|
|
|
|
|
|
|
|
$retval = [];
|
|
|
|
$retval['h'] = (int) $t->format('%h');
|
|
|
|
$retval['m'] = (int) $t->format('%i');
|
|
|
|
$retval['s'] = (int) $t->format('%s');
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert seconds to HH MM format
|
|
|
|
*
|
|
|
|
* @param int $seconds
|
|
|
|
* @param bool $incl_sec
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function secondsToTimeString($seconds, $incl_sec = false): string
|
|
|
|
{
|
|
|
|
$hms = self::secondsToTimeParts($seconds);
|
|
|
|
$format = $hms['h'].'h '.$hms['m'].'m';
|
|
|
|
if ($incl_sec) {
|
|
|
|
$format .= ' '.$hms['s'].'s';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $minutes
|
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function minutesToSeconds($minutes)
|
|
|
|
{
|
|
|
|
return $minutes * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the seconds to minutes and then round it up
|
|
|
|
*
|
|
|
|
* @param $seconds
|
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function secondsToMinutes($seconds)
|
|
|
|
{
|
|
|
|
return ceil($seconds / 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert hours to minutes. Pretty complex
|
|
|
|
*
|
|
|
|
* @param $minutes
|
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function minutesToHours($minutes)
|
|
|
|
{
|
|
|
|
return $minutes / 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $hours
|
|
|
|
* @param null $minutes
|
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function hoursToMinutes($hours, $minutes = null)
|
|
|
|
{
|
|
|
|
$total = (int) $hours * 60;
|
|
|
|
if ($minutes) {
|
|
|
|
$total += (int) $minutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $total;
|
|
|
|
}
|
2018-02-11 11:36:13 +08:00
|
|
|
}
|