| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Duration |
| 5 |
*/ |
| 6 |
class LP_Duration { |
| 7 |
/** |
| 8 |
* @var int |
| 9 |
*/ |
| 10 |
protected $_duration = 0; |
| 11 |
|
| 12 |
/** |
| 13 |
* LP_Duration constructor. |
| 14 |
* |
| 15 |
* @param mixed $duration |
| 16 |
*/ |
| 17 |
public function __construct( $duration ) { |
| 18 |
if ( is_numeric( $duration ) ) { |
| 19 |
if ( $duration < 0 ) { |
| 20 |
$duration = 0; |
| 21 |
} |
| 22 |
$this->_duration = absint( $duration ); |
| 23 |
} else { |
| 24 |
if ( preg_match( '~([0-9]+) (second|minute|hour|day|week)~', $duration, $m ) ) { |
| 25 |
$s = array( |
| 26 |
'second' => 1, |
| 27 |
'minute' => 60, |
| 28 |
'hour' => 3600, |
| 29 |
'day' => 3600 * 24, |
| 30 |
'week' => 3600 * 24 * 7, |
| 31 |
'month' => 3600 * 30, |
| 32 |
); |
| 33 |
$this->_duration = $m[1] * $s[ $m[2] ]; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param bool $leading_zero |
| 40 |
* |
| 41 |
* @return int |
| 42 |
*/ |
| 43 |
public function get_weeks( $leading_zero = false ) { |
| 44 |
$weeks = $this->_duration ? ( $this->_duration - $this->_duration % ( 3600 * 24 * 7 ) ) / ( 3600 * 24 * 7 ) : 0; |
| 45 |
|
| 46 |
return $weeks < 10 && $leading_zero ? "0{$weeks}" : $weeks; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get number of days. |
| 51 |
* |
| 52 |
* @param bool $leading_zero |
| 53 |
* |
| 54 |
* @return int |
| 55 |
*/ |
| 56 |
public function get_days( $leading_zero = false ) { |
| 57 |
$days = $this->_duration ? ( $this->_duration - $this->_duration % ( 3600 * 24 ) ) / ( 3600 * 24 ) : 0; |
| 58 |
|
| 59 |
return $days < 10 && $leading_zero ? "0{$days}" : $days; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get number of hours. |
| 64 |
* |
| 65 |
* @param bool $leading_zero |
| 66 |
* |
| 67 |
* @return int |
| 68 |
*/ |
| 69 |
public function get_hours( $leading_zero = false ) { |
| 70 |
$hours = $this->_duration ? ( $this->_duration - $this->_duration % 3600 ) / 3600 : 0; |
| 71 |
|
| 72 |
return $hours < 10 && $leading_zero ? "0{$hours}" : $hours; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get number of minutes. |
| 77 |
* |
| 78 |
* @param bool $leading_zero |
| 79 |
* |
| 80 |
* @return int |
| 81 |
*/ |
| 82 |
public function get_minutes( $leading_zero = false ) { |
| 83 |
$minutes = $this->_duration ? ( $this->_duration - $this->_duration % 60 ) / 60 : 0; |
| 84 |
|
| 85 |
return $minutes < 10 && $leading_zero ? "0{$minutes}" : $minutes; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get number of seconds. |
| 90 |
* |
| 91 |
* @param bool $leading_zero |
| 92 |
* |
| 93 |
* @return int|string |
| 94 |
*/ |
| 95 |
public function get_seconds( $leading_zero = false ) { |
| 96 |
return $this->_duration < 10 && $leading_zero ? '0' . $this->_duration : $this->_duration; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param array|string $format |
| 101 |
* @param bool $remove_empty |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public function to_timer( $format = '', $remove_empty = false ) { |
| 106 |
$day = $this->get_days(); |
| 107 |
$mod = $this->_duration - $day * 24 * 3600; |
| 108 |
$hour = ( $mod - ( $mod % 3600 ) ) / 3600; |
| 109 |
$mod = $mod - $hour * 3600; |
| 110 |
$minute = ( $mod - $mod % 60 ) / 60; |
| 111 |
$second = $mod - $minute * 60; |
| 112 |
|
| 113 |
$parts = array(); |
| 114 |
|
| 115 |
if ( $day ) { |
| 116 |
$parts['day'] = $day; |
| 117 |
} |
| 118 |
|
| 119 |
if ( $hour ) { |
| 120 |
$parts['hour'] = $hour; |
| 121 |
} else { |
| 122 |
$parts['hour'] = 0; |
| 123 |
} |
| 124 |
|
| 125 |
$parts['minute'] = $minute; |
| 126 |
$parts['second'] = $second; |
| 127 |
|
| 128 |
foreach ( $parts as $k => $v ) { |
| 129 |
if ( $v < 10 ) { |
| 130 |
$parts[ $k ] = "0{$v}"; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ( $format ) { |
| 135 |
foreach ( array( 'day', 'hour', 'minute', 'second' ) as $p ) { |
| 136 |
if ( $remove_empty && array_key_exists( $p, $parts ) && intval( $parts[ $p ] ) == 0 ) { |
| 137 |
unset( $parts[ $p ] ); |
| 138 |
} |
| 139 |
if ( ! empty( $format[ $p ] ) && ! empty( $parts[ $p ] ) ) { |
| 140 |
$parts[ $p ] = sprintf( $format[ $p ], $parts[ $p ] ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return join( ' ', $parts ); |
| 145 |
} |
| 146 |
|
| 147 |
return join( ':', $parts ); |
| 148 |
} |
| 149 |
|
| 150 |
public function __toString() { |
| 151 |
return $this->_duration . ''; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param LP_Duration|int $duration |
| 156 |
* |
| 157 |
* @return LP_Duration |
| 158 |
*/ |
| 159 |
public function diff( $duration ) { |
| 160 |
$diff = $duration instanceof LP_Duration ? $this->_duration - $duration->get_seconds() : $this->_duration - $duration; |
| 161 |
|
| 162 |
return new LP_Duration( $diff ); |
| 163 |
} |
| 164 |
|
| 165 |
public function get() { |
| 166 |
return $this->_duration; |
| 167 |
} |
| 168 |
} |
| 169 |
|