@@ -50,6 +50,10 @@ |
||
| 50 | 50 | $this->restoreTimezone(); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | + /** |
|
| 54 | + * @param integer $past |
|
| 55 | + * @param integer $now |
|
| 56 | + */ |
|
| 53 | 57 | public function inStamp($past, $now = null) { |
| 54 | 58 | if($now==null){ |
| 55 | 59 | $now=time(); |
@@ -10,15 +10,15 @@ discard block |
||
| 10 | 10 | class TimeAgo |
| 11 | 11 | { |
| 12 | 12 | // defines the number of seconds per "unit" |
| 13 | - private $secondsPerMinute = 60; |
|
| 14 | - private $secondsPerHour = 3600; |
|
| 15 | - private $secondsPerDay = 86400; |
|
| 16 | - private $secondsPerMonth = 2592000; |
|
| 17 | - private $secondsPerYear = 31536000; // 31622400 seconds on leap years though... |
|
| 13 | + private $secondsPerMinute=60; |
|
| 14 | + private $secondsPerHour=3600; |
|
| 15 | + private $secondsPerDay=86400; |
|
| 16 | + private $secondsPerMonth=2592000; |
|
| 17 | + private $secondsPerYear=31536000; // 31622400 seconds on leap years though... |
|
| 18 | 18 | private $timezone; |
| 19 | 19 | private $previousTimezone; |
| 20 | 20 | |
| 21 | - private static $timeAgoStrings = array( |
|
| 21 | + private static $timeAgoStrings=array( |
|
| 22 | 22 | 'aboutOneDay' => "1 天前", |
| 23 | 23 | 'aboutOneHour' => "大约 1 小时前", |
| 24 | 24 | 'aboutOneMonth' => "大约 1 个月前", |
@@ -36,9 +36,9 @@ discard block |
||
| 36 | 36 | * TimeAgo constructor. |
| 37 | 37 | * @param null|DateTimeZone $timezone the timezone to use (uses system if none is given) |
| 38 | 38 | */ |
| 39 | - public function __construct($timezone = null) |
|
| 39 | + public function __construct($timezone=null) |
|
| 40 | 40 | { |
| 41 | - $this->timezone = $timezone; |
|
| 41 | + $this->timezone=$timezone; |
|
| 42 | 42 | // sets the default timezone |
| 43 | 43 | $this->changeTimezone(); |
| 44 | 44 | } |
@@ -50,84 +50,84 @@ discard block |
||
| 50 | 50 | $this->restoreTimezone(); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function inStamp($past, $now = null) { |
|
| 54 | - if($now==null){ |
|
| 53 | + public function inStamp($past, $now=null) { |
|
| 54 | + if ($now == null) { |
|
| 55 | 55 | $now=time(); |
| 56 | 56 | } |
| 57 | 57 | // creates the "time ago" string. This always starts with an "about..." |
| 58 | - $timeAgo = ""; |
|
| 58 | + $timeAgo=""; |
|
| 59 | 59 | |
| 60 | 60 | // finds the time difference |
| 61 | - $timeDifference = $now - $past; |
|
| 61 | + $timeDifference=$now - $past; |
|
| 62 | 62 | // rule 0 |
| 63 | 63 | // $past is null or empty or '' |
| 64 | - if ($past==0) { |
|
| 65 | - $timeAgo = $this->translate('never'); |
|
| 64 | + if ($past == 0) { |
|
| 65 | + $timeAgo=$this->translate('never'); |
|
| 66 | 66 | } |
| 67 | 67 | // rule 1 |
| 68 | 68 | // less than 29secs |
| 69 | 69 | else if ($this->isLessThan29Seconds($timeDifference)) { |
| 70 | - $timeAgo = $this->translate('lessThanAMinute'); |
|
| 70 | + $timeAgo=$this->translate('lessThanAMinute'); |
|
| 71 | 71 | } |
| 72 | 72 | // rule 2 |
| 73 | 73 | // more than 29secs and less than 1min29secss |
| 74 | 74 | else if ($this->isLessThan1Min29Seconds($timeDifference)) { |
| 75 | - $timeAgo = $this->translate('oneMinute'); |
|
| 75 | + $timeAgo=$this->translate('oneMinute'); |
|
| 76 | 76 | } |
| 77 | 77 | // rule 3 |
| 78 | 78 | // between 1min30secs and 44mins29secs |
| 79 | 79 | else if ($this->isLessThan44Min29Secs($timeDifference)) { |
| 80 | - $minutes = round($timeDifference / $this->secondsPerMinute); |
|
| 81 | - $timeAgo = $this->translate('lessThanOneHour', $minutes); |
|
| 80 | + $minutes=round($timeDifference / $this->secondsPerMinute); |
|
| 81 | + $timeAgo=$this->translate('lessThanOneHour', $minutes); |
|
| 82 | 82 | } |
| 83 | 83 | // rule 4 |
| 84 | 84 | // between 44mins30secs and 1hour29mins59secs |
| 85 | 85 | else if ($this->isLessThan1Hour29Mins59Seconds($timeDifference)) { |
| 86 | - $timeAgo = $this->translate('aboutOneHour'); |
|
| 86 | + $timeAgo=$this->translate('aboutOneHour'); |
|
| 87 | 87 | } |
| 88 | 88 | // rule 5 |
| 89 | 89 | // between 1hour29mins59secs and 23hours59mins29secs |
| 90 | 90 | else if ($this->isLessThan23Hours59Mins29Seconds($timeDifference)) { |
| 91 | - $hours = round($timeDifference / $this->secondsPerHour); |
|
| 92 | - $timeAgo = $this->translate('hours', $hours); |
|
| 91 | + $hours=round($timeDifference / $this->secondsPerHour); |
|
| 92 | + $timeAgo=$this->translate('hours', $hours); |
|
| 93 | 93 | } |
| 94 | 94 | // rule 6 |
| 95 | 95 | // between 23hours59mins30secs and 47hours59mins29secs |
| 96 | 96 | else if ($this->isLessThan47Hours59Mins29Seconds($timeDifference)) { |
| 97 | - $timeAgo = $this->translate('aboutOneDay'); |
|
| 97 | + $timeAgo=$this->translate('aboutOneDay'); |
|
| 98 | 98 | } |
| 99 | 99 | // rule 7 |
| 100 | 100 | // between 47hours59mins30secs and 29days23hours59mins29secs |
| 101 | 101 | else if ($this->isLessThan29Days23Hours59Mins29Seconds($timeDifference)) { |
| 102 | - $days = round($timeDifference / $this->secondsPerDay); |
|
| 103 | - $timeAgo = $this->translate('days', $days); |
|
| 102 | + $days=round($timeDifference / $this->secondsPerDay); |
|
| 103 | + $timeAgo=$this->translate('days', $days); |
|
| 104 | 104 | } |
| 105 | 105 | // rule 8 |
| 106 | 106 | // between 29days23hours59mins30secs and 59days23hours59mins29secs |
| 107 | 107 | else if ($this->isLessThan59Days23Hours59Mins29Secs($timeDifference)) { |
| 108 | - $timeAgo = $this->translate('aboutOneMonth'); |
|
| 108 | + $timeAgo=$this->translate('aboutOneMonth'); |
|
| 109 | 109 | } |
| 110 | 110 | // rule 9 |
| 111 | 111 | // between 59days23hours59mins30secs and 1year (minus 1sec) |
| 112 | 112 | else if ($this->isLessThan1Year($timeDifference)) { |
| 113 | - $months = round($timeDifference / $this->secondsPerMonth); |
|
| 113 | + $months=round($timeDifference / $this->secondsPerMonth); |
|
| 114 | 114 | // if months is 1, then set it to 2, because we are "past" 1 month |
| 115 | 115 | if ($months == 1) { |
| 116 | - $months = 2; |
|
| 116 | + $months=2; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $timeAgo = $this->translate('months', $months); |
|
| 119 | + $timeAgo=$this->translate('months', $months); |
|
| 120 | 120 | } |
| 121 | 121 | // rule 10 |
| 122 | 122 | // between 1year and 2years (minus 1sec) |
| 123 | 123 | else if ($this->isLessThan2Years($timeDifference)) { |
| 124 | - $timeAgo = $this->translate('aboutOneYear'); |
|
| 124 | + $timeAgo=$this->translate('aboutOneYear'); |
|
| 125 | 125 | } |
| 126 | 126 | // rule 11 |
| 127 | 127 | // 2years or more |
| 128 | 128 | else { |
| 129 | - $years = floor($timeDifference / $this->secondsPerYear); |
|
| 130 | - $timeAgo = $this->translate('years', $years); |
|
| 129 | + $years=floor($timeDifference / $this->secondsPerYear); |
|
| 130 | + $timeAgo=$this->translate('years', $years); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | return $timeAgo; |
@@ -139,18 +139,18 @@ discard block |
||
| 139 | 139 | * @param string $now the current time, defaults to now (can be an other time though) |
| 140 | 140 | * @return string the difference in spoken format, e.g. 1 day ago |
| 141 | 141 | */ |
| 142 | - public function inWords($past, $now = "now") |
|
| 142 | + public function inWords($past, $now="now") |
|
| 143 | 143 | { |
| 144 | 144 | |
| 145 | 145 | // finds the past in datetime |
| 146 | - $past = strtotime($past); |
|
| 146 | + $past=strtotime($past); |
|
| 147 | 147 | // finds the current datetime |
| 148 | - $now = strtotime($now); |
|
| 148 | + $now=strtotime($now); |
|
| 149 | 149 | if ($this->isPastEmpty($past)) { |
| 150 | 150 | $past=0; |
| 151 | 151 | $past=0; |
| 152 | 152 | } |
| 153 | - return $this->inStamp($past,$now); |
|
| 153 | + return $this->inStamp($past, $now); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
@@ -161,23 +161,23 @@ discard block |
||
| 161 | 161 | * @param string $now the "now" time to parse |
| 162 | 162 | * @return array the difference in dates, using the two dates |
| 163 | 163 | */ |
| 164 | - public function dateDifference($past, $now = "now") |
|
| 164 | + public function dateDifference($past, $now="now") |
|
| 165 | 165 | { |
| 166 | 166 | // initializes the placeholders for the different "times" |
| 167 | - $seconds = 0; |
|
| 168 | - $minutes = 0; |
|
| 169 | - $hours = 0; |
|
| 170 | - $days = 0; |
|
| 171 | - $months = 0; |
|
| 172 | - $years = 0; |
|
| 167 | + $seconds=0; |
|
| 168 | + $minutes=0; |
|
| 169 | + $hours=0; |
|
| 170 | + $days=0; |
|
| 171 | + $months=0; |
|
| 172 | + $years=0; |
|
| 173 | 173 | |
| 174 | 174 | // finds the past in datetime |
| 175 | - $past = strtotime($past); |
|
| 175 | + $past=strtotime($past); |
|
| 176 | 176 | // finds the current datetime |
| 177 | - $now = strtotime($now); |
|
| 177 | + $now=strtotime($now); |
|
| 178 | 178 | |
| 179 | 179 | // calculates the difference |
| 180 | - $timeDifference = $now - $past; |
|
| 180 | + $timeDifference=$now - $past; |
|
| 181 | 181 | |
| 182 | 182 | // starts determining the time difference |
| 183 | 183 | if ($timeDifference >= 0) { |
@@ -185,46 +185,46 @@ discard block |
||
| 185 | 185 | // finds the number of years |
| 186 | 186 | case ($timeDifference >= $this->secondsPerYear): |
| 187 | 187 | // uses floor to remove decimals |
| 188 | - $years = floor($timeDifference / $this->secondsPerYear); |
|
| 188 | + $years=floor($timeDifference / $this->secondsPerYear); |
|
| 189 | 189 | // saves the amount of seconds left |
| 190 | - $timeDifference = $timeDifference - ($years * $this->secondsPerYear); |
|
| 190 | + $timeDifference=$timeDifference - ($years * $this->secondsPerYear); |
|
| 191 | 191 | |
| 192 | 192 | // finds the number of months |
| 193 | 193 | case ($timeDifference >= $this->secondsPerMonth && $timeDifference <= ($this->secondsPerYear - 1)): |
| 194 | 194 | // uses floor to remove decimals |
| 195 | - $months = floor($timeDifference / $this->secondsPerMonth); |
|
| 195 | + $months=floor($timeDifference / $this->secondsPerMonth); |
|
| 196 | 196 | // saves the amount of seconds left |
| 197 | - $timeDifference = $timeDifference - ($months * $this->secondsPerMonth); |
|
| 197 | + $timeDifference=$timeDifference - ($months * $this->secondsPerMonth); |
|
| 198 | 198 | |
| 199 | 199 | // finds the number of days |
| 200 | 200 | case ($timeDifference >= $this->secondsPerDay && $timeDifference <= ($this->secondsPerYear - 1)): |
| 201 | 201 | // uses floor to remove decimals |
| 202 | - $days = floor($timeDifference / $this->secondsPerDay); |
|
| 202 | + $days=floor($timeDifference / $this->secondsPerDay); |
|
| 203 | 203 | // saves the amount of seconds left |
| 204 | - $timeDifference = $timeDifference - ($days * $this->secondsPerDay); |
|
| 204 | + $timeDifference=$timeDifference - ($days * $this->secondsPerDay); |
|
| 205 | 205 | |
| 206 | 206 | // finds the number of hours |
| 207 | 207 | case ($timeDifference >= $this->secondsPerHour && $timeDifference <= ($this->secondsPerDay - 1)): |
| 208 | 208 | // uses floor to remove decimals |
| 209 | - $hours = floor($timeDifference / $this->secondsPerHour); |
|
| 209 | + $hours=floor($timeDifference / $this->secondsPerHour); |
|
| 210 | 210 | // saves the amount of seconds left |
| 211 | - $timeDifference = $timeDifference - ($hours * $this->secondsPerHour); |
|
| 211 | + $timeDifference=$timeDifference - ($hours * $this->secondsPerHour); |
|
| 212 | 212 | |
| 213 | 213 | // finds the number of minutes |
| 214 | 214 | case ($timeDifference >= $this->secondsPerMinute && $timeDifference <= ($this->secondsPerHour - 1)): |
| 215 | 215 | // uses floor to remove decimals |
| 216 | - $minutes = floor($timeDifference / $this->secondsPerMinute); |
|
| 216 | + $minutes=floor($timeDifference / $this->secondsPerMinute); |
|
| 217 | 217 | // saves the amount of seconds left |
| 218 | - $timeDifference = $timeDifference - ($minutes * $this->secondsPerMinute); |
|
| 218 | + $timeDifference=$timeDifference - ($minutes * $this->secondsPerMinute); |
|
| 219 | 219 | |
| 220 | 220 | // finds the number of seconds |
| 221 | 221 | case ($timeDifference <= ($this->secondsPerMinute - 1)): |
| 222 | 222 | // seconds is just what there is in the timeDifference variable |
| 223 | - $seconds = $timeDifference; |
|
| 223 | + $seconds=$timeDifference; |
|
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - $difference = [ |
|
| 227 | + $difference=[ |
|
| 228 | 228 | "years" => $years, |
| 229 | 229 | "months" => $months, |
| 230 | 230 | "days" => $days, |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | * @param string $time the time to add to the translated text. |
| 243 | 243 | * @return string the translated label text including the time. |
| 244 | 244 | */ |
| 245 | - protected function translate($label, $time = '') |
|
| 245 | + protected function translate($label, $time='') |
|
| 246 | 246 | { |
| 247 | 247 | // handles a usecase introduced in #18, where a new translation was added. |
| 248 | 248 | // This would cause an array-out-of-bound exception, since the index does not |
@@ -260,9 +260,9 @@ discard block |
||
| 260 | 260 | */ |
| 261 | 261 | protected function changeTimezone() |
| 262 | 262 | { |
| 263 | - $this->previousTimezone = false; |
|
| 263 | + $this->previousTimezone=false; |
|
| 264 | 264 | if ($this->timezone) { |
| 265 | - $this->previousTimezone = date_default_timezone_get(); |
|
| 265 | + $this->previousTimezone=date_default_timezone_get(); |
|
| 266 | 266 | date_default_timezone_set($this->timezone); |
| 267 | 267 | } |
| 268 | 268 | } |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | { |
| 275 | 275 | if ($this->previousTimezone) { |
| 276 | 276 | date_default_timezone_set($this->previousTimezone); |
| 277 | - $this->previousTimezone = false; |
|
| 277 | + $this->previousTimezone=false; |
|
| 278 | 278 | } |
| 279 | 279 | } |
| 280 | 280 | |