DateTimeHelper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 85
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeTimestamp() 0 16 4
C seconds() 0 52 12
1
<?php
2
/*
3
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2013 Gamer Network Ltd.
6
 *
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-core
10
 */
11
12
namespace yolk\helpers;
13
14
class DateTimeHelper {
15
16
	/**
17
	 * Convert a value to a timestamp.
18
	 * @param  mixed $time
19
	 * @return integer
20
	 */
21
	public static function makeTimestamp( $time ) {
22
23
		if( is_numeric($time) )
24
			return (int) $time;
25
26
		elseif( $time instanceof \DateTime )
27
			return $time->getTimestamp();
28
29
		$ts = strtotime($time);
30
31
		if( $ts === false )
32
			throw new \LogicException("Unable convert {$time} to a valid timestamp");
33
34
		return $ts;
35
36
	}
37
38
	/**
39
	 * Converts a string representation containing one or more of hours, minutes and seconds into a total number of seconds.
40
	 * e.g. seconds("3 hours 4 minutes 10 seconds"), seconds("5min"), seconds("4.5h")
41
	 *
42
	 * @param  string  $str   string to convert
43
	 * @return integer|float
44
	 */
45
	public static function seconds( $str ) {
46
47
		$hours   = 0;
48
		$minutes = 0;
49
		$seconds = 0;
50
51
		if( preg_match('/^\d+:\d+$/', $str) ) {
52
			list(, $minutes, $seconds) = explode(':', $str);
53
		}
54
		elseif( preg_match('/^\d+:\d+:\d+$/', $str) ) {
55
			list($hours, $minutes, $seconds) = explode(':', $str);
56
		}
57
		else {
58
59
			// convert invalid characters to spaces
60
			$str = preg_replace('/[^a-z0-9. ]+/iu', ' ', $str);
61
62
			// strip multiple spaces
63
			$str = preg_replace('/ {2,}/u', ' ', $str);
64
65
			// compress scales and units together so '2 hours' => '2hours'
66
			$str = preg_replace('/([0-9.]+) ([cdehimnorstu]+)/u', '$1$2', $str);
67
68
			foreach( explode(' ', $str) as $item ) {
69
70
				if( !preg_match('/^([0-9.]+)([cdehimnorstu]+)$/u', $item, $m) )
71
					return false;
72
73
				list(, $scale, $unit) = $m;
74
75
				$scale = ((float) $scale != (int) $scale) ? (float) $scale : (int) $scale;
76
77
				if( preg_match('/^h(r|our|ours)?$/u', $unit) && !$hours ) {
78
					$hours = $scale;
79
				}
80
				elseif( preg_match('/^m(in|ins|inute|inutes)?$/u', $unit) && !$minutes ) {
81
					$minutes = $scale;
82
				}
83
				elseif( preg_match('/^s(ec|ecs|econd|econds)?$/u', $unit) && !$seconds ) {
84
					$seconds = $scale;
85
				}
86
				else {
87
					return false;
88
				}
89
90
			}
91
92
		}
93
94
		return ($hours * 3600) + ($minutes * 60) + $seconds;
95
96
	}
97
98
}
99
100
// EOF