| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\helpers;
- use Exception;
- class TimeUnitsAliassesHelper
- {
- private static $units = [
- 'M' => 60,
- 'H' => 3600,
- 'D' => 86400,
- 'W' => 604800
- ];
- /**
- * @param $val
- * @return float|int
- * @throws Exception if can't parse $val to seconds
- */
- public static function convertToSeconds( $val )
- {
- if (preg_match('/^\d+$/', $val))
- {
- return (int)$val;
- }
- preg_match('/(\d+)([MHDW])/i', $val, $matches);
- if (!$matches)
- {
- throw new Exception('Wrong TTL Syntax for: '.$val);
- }
- $secondsPerSingeUnit = static::$units[strtoupper($matches[2])];
- $amount = (int)$matches[1];
- return $secondsPerSingeUnit * $amount;
- }
- }
|