phpvms/app/Facades/Utils.php

70 lines
1.7 KiB
PHP
Raw Normal View History

2017-07-04 11:53:49 +08:00
<?php
namespace App\Facades;
use \Illuminate\Support\Facades\Facade;
class Utils extends Facade
{
2017-12-04 04:34:32 +08:00
/**
* Convert seconds to an array of hours, minutes, seconds
* @param $seconds
* @return array['h', 'm', 's']
*/
public static function secondsToTimeParts($seconds): array
{
2017-07-04 11:53:49 +08:00
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
2017-12-04 04:34:32 +08:00
$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 $seconds
* @param bool $incl_sec
* @return string
*/
public static function secondsToTime($seconds, $incl_sec=false): string
{
$hms = self::secondsToTimeParts($seconds);
$format = $hms['h'].'h '.$hms['m'].'m';
if($incl_sec) {
2017-12-04 04:34:32 +08:00
$format .= ' '.$hms['s'].'s';
}
2017-12-04 04:34:32 +08:00
return $format;
2017-07-04 11:53:49 +08:00
}
/**
* Bitwise operator for setting days of week to integer field
* @param int $datefield initial datefield
* @param array $day_enums Array of values from config("enum.days")
* @return int
*/
public static function setDays(int $datefield, array $day_enums) {
foreach($day_enums as $day) {
$datefield |= $day;
}
return $datefield;
}
/**
* Bit check if a day exists within a integer bitfield
* @param int $datefield datefield from database
* @param int $day_enum Value from config("enum.days")
* @return bool
*/
public static function hasDay(int $datefield, int $day_enum) {
return ($datefield & $day_enum) === $datefield;
}
2017-07-04 11:53:49 +08:00
}