[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: time.php 4409 2007-02-02 13:20:59Z phpnut $ */ 3 /** 4 * Time Helper class file. 5 * 6 * PHP versions 4 and 5 7 * 8 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 9 * Copyright 2005-2007, Cake Software Foundation, Inc. 10 * 1785 E. Sahara Avenue, Suite 490-204 11 * Las Vegas, Nevada 89104 12 * 13 * Licensed under The MIT License 14 * Redistributions of files must retain the above copyright notice. 15 * 16 * @filesource 17 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 18 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 19 * @package cake 20 * @subpackage cake.cake.libs.view.helpers 21 * @since CakePHP(tm) v 0.10.0.1076 22 * @version $Revision: 4409 $ 23 * @modifiedby $LastChangedBy: phpnut $ 24 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 25 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 26 */ 27 /** 28 * Time Helper class for easy use of time data. 29 * 30 * Manipulation of time data. 31 * 32 * @package cake 33 * @subpackage cake.cake.libs.view.helpers 34 */ 35 class TimeHelper extends Helper { 36 /** 37 * Returns given string trimmed to given length, adding an ending (default: "..") if necessary. 38 * 39 * @param string $string String to trim 40 * @param integer $length Length of returned string, excluding ellipsis 41 * @param string $ending Ending to be appended after trimmed string 42 * @return string Trimmed string 43 * @access public 44 */ 45 function trim($string, $length, $ending = '..') { 46 return substr($string, 0, $length) . (strlen($string) > $length ? $ending : null); 47 } 48 /** 49 * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. 50 * 51 * @param string $date_string Datetime string 52 * @return string Formatted date string 53 * @access public 54 */ 55 function fromString($date_string) { 56 if (is_integer($date_string) || is_numeric($date_string)) { 57 return intval($date_string); 58 } else { 59 return strtotime($date_string); 60 } 61 } 62 /** 63 * Returns a nicely formatted date string for given Datetime string. 64 * 65 * @param string $date_string Datetime string or Unix timestamp 66 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 67 * @return string Formatted date string 68 * @access public 69 */ 70 function nice($date_string = null, $return = false) { 71 if ($date_string != null) { 72 $date = $this->fromString($date_string); 73 } else { 74 $date = time(); 75 } 76 77 $ret = date("D, M jS Y, H:i", $date); 78 return $this->output($ret, $return); 79 } 80 /** 81 * Returns a formatted descriptive date string for given datetime string. 82 * 83 * If the given date is today, the returned string could be "Today, 16:54". 84 * If the given date was yesterday, the returned string could be "Yesterday, 16:54". 85 * If $date_string's year is the current year, the returned string does not 86 * include mention of the year. 87 * 88 * @param string $date_string Datetime string or Unix timestamp 89 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 90 * @return string Described, relative date string 91 * @access public 92 */ 93 function niceShort($date_string = null, $return = false) { 94 $date = $date_string ? $this->fromString($date_string) : time(); 95 $y = $this->isThisYear($date) ? '' : ' Y'; 96 97 if ($this->isToday($date)) { 98 $ret = "Today, " . date("H:i", $date); 99 } elseif($this->wasYesterday($date)) { 100 $ret = "Yesterday, " . date("H:i", $date); 101 } else { 102 $ret = date("M jS{$y}, H:i", $date); 103 } 104 105 return $this->output($ret, $return); 106 } 107 /** 108 * Returns true if given datetime string is today. 109 * 110 * @param string $date_string Datetime string or Unix timestamp 111 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 112 * @return boolean True if datetime string is today 113 * @access public 114 */ 115 function isToday($date_string, $return = false) { 116 $date = $this->fromString($date_string); 117 $ret = date('Y-m-d', $date) == date('Y-m-d', time()); 118 return $this->output($ret, $return); 119 } 120 /** 121 * Returns a partial SQL string to search for all records between two dates. 122 * 123 * @param string $date_string Datetime string or Unix timestamp 124 * @param string $end Datetime string or Unix timestamp 125 * @param string $field_name Name of database field to compare with 126 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 127 * @return string Partial SQL string. 128 * @access public 129 */ 130 function daysAsSql($begin, $end, $field_name, $return = false) { 131 $begin = $this->fromString($begin); 132 $end = $this->fromString($end); 133 $begin = date('Y-m-d', $begin) . ' 00:00:00'; 134 $end = date('Y-m-d', $end) . ' 23:59:59'; 135 136 return $this->output("($field_name >= '$begin') AND ($field_name <= '$end')", $return); 137 } 138 /** 139 * Returns a partial SQL string to search for all records between two times 140 * occurring on the same day. 141 * 142 * @param string $date_string Datetime string or Unix timestamp 143 * @param string $field_name Name of database field to compare with 144 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 145 * @return string Partial SQL string. 146 * @access public 147 */ 148 function dayAsSql($date_string, $field_name, $return = false) { 149 $date = $this->fromString($date_string); 150 $ret = $this->daysAsSql($date_string, $date_string, $field_name); 151 return $this->output($ret, $return); 152 } 153 /** 154 * Returns true if given datetime string is within current year. 155 * 156 * @param string $date_string Datetime string or Unix timestamp 157 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 158 * @return boolean True if datetime string is within current year 159 * @access public 160 */ 161 function isThisYear($date_string, $return = false) { 162 $date = $this->fromString($date_string); 163 $ret = date('Y', $date) == date('Y', time()); 164 return $this->output($ret, $return); 165 } 166 /** 167 * Returns true if given datetime string was yesterday. 168 * 169 * @param string $date_string Datetime string or Unix timestamp 170 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 171 * @return boolean True if datetime string was yesterday 172 * @access public 173 */ 174 function wasYesterday($date_string, $return = false) { 175 $date = $this->fromString($date_string); 176 $ret = date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday')); 177 return $this->output($ret, $return); 178 } 179 /** 180 * Returns true if given datetime string is tomorrow. 181 * 182 * @param string $date_string Datetime string or Unix timestamp 183 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 184 * @return boolean True if datetime string was yesterday 185 * @access public 186 */ 187 function isTomorrow($date_string, $return = false) { 188 $date = $this->fromString($date_string); 189 $ret = date('Y-m-d', $date) == date('Y-m-d', strtotime('tomorrow')); 190 return $this->output($ret, $return); 191 } 192 /** 193 * Returns a UNIX timestamp from a textual datetime description. Wrapper for PHP function strtotime(). 194 * 195 * @param string $date_string Datetime string to be represented as a Unix timestamp 196 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 197 * @return int Unix timestamp 198 * @access public 199 */ 200 function toUnix($date_string, $return = false) { 201 $ret = strtotime($date_string); 202 return $this->output($ret, $return); 203 } 204 /** 205 * Returns a date formatted for Atom RSS feeds. 206 * 207 * @param string $date_string Datetime string or Unix timestamp 208 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 209 * @return string Formatted date string 210 * @access public 211 */ 212 function toAtom($date_string, $return = false) { 213 $date = $this->fromString($date_string); 214 $ret = date('Y-m-d\TH:i:s\Z', $date); 215 return $this->output($ret, $return); 216 } 217 /** 218 * Formats date for RSS feeds 219 * 220 * @param string $date_string Datetime string or Unix timestamp 221 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 222 * @return string Formatted date string 223 * @access public 224 */ 225 function toRSS($date_string, $return = false) { 226 $date = $this->fromString($date_string); 227 $ret = date("r", $date); 228 return $this->output($ret, $return); 229 } 230 /** 231 * Returns either a relative date or a formatted date depending 232 * on the difference between the current time and given datetime. 233 * $datetime should be in a <i>strtotime</i>-parsable format, like MySQL's datetime datatype. 234 * 235 * Relative dates look something like this: 236 * 3 weeks, 4 days ago 237 * 15 seconds ago 238 * Formatted dates look like this: 239 * on 02/18/2004 240 * 241 * The returned string includes 'ago' or 'on' and assumes you'll properly add a word 242 * like 'Posted ' before the function output. 243 * 244 * @param string $date_string Datetime string or Unix timestamp 245 * @param string $format Default format if timestamp is used in $date_string 246 * @param string $backwards False if $date_string is in the past, true if in the future 247 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 248 * @return string Relative time string. 249 * @access public 250 */ 251 function timeAgoInWords($datetime_string, $format = 'j/n/y', $backwards = false, $return = false) { 252 $datetime = $this->fromString($datetime_string); 253 254 $in_seconds = $datetime; 255 if ($backwards) { 256 $diff = $in_seconds - time(); 257 } else { 258 $diff = time() - $in_seconds; 259 } 260 261 $months = floor($diff / 2419200); 262 $diff -= $months * 2419200; 263 $weeks = floor($diff / 604800); 264 $diff -= $weeks * 604800; 265 $days = floor($diff / 86400); 266 $diff -= $days * 86400; 267 $hours = floor($diff / 3600); 268 $diff -= $hours * 3600; 269 $minutes = floor($diff / 60); 270 $diff -= $minutes * 60; 271 $seconds = $diff; 272 273 if ($months > 0) { 274 // over a month old, just show date (mm/dd/yyyy format) 275 $relative_date = 'on ' . date($format, $in_seconds); 276 $old = true; 277 } else { 278 $relative_date = ''; 279 $old = false; 280 281 if ($weeks > 0) { 282 // weeks and days 283 $relative_date .= ($relative_date ? ', ' : '') . $weeks . ' week' . ($weeks > 1 ? 's' : ''); 284 $relative_date .= $days > 0 ? ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : '') : ''; 285 } elseif($days > 0) { 286 // days and hours 287 $relative_date .= ($relative_date ? ', ' : '') . $days . ' day' . ($days > 1 ? 's' : ''); 288 $relative_date .= $hours > 0 ? ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : '') : ''; 289 } elseif($hours > 0) { 290 // hours and minutes 291 $relative_date .= ($relative_date ? ', ' : '') . $hours . ' hour' . ($hours > 1 ? 's' : ''); 292 $relative_date .= $minutes > 0 ? ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : '') : ''; 293 } elseif($minutes > 0) { 294 // minutes only 295 $relative_date .= ($relative_date ? ', ' : '') . $minutes . ' minute' . ($minutes > 1 ? 's' : ''); 296 } else { 297 // seconds only 298 $relative_date .= ($relative_date ? ', ' : '') . $seconds . ' second' . ($seconds != 1 ? 's' : ''); 299 } 300 } 301 302 $ret = $relative_date; 303 304 // show relative date and add proper verbiage 305 if (!$backwards && !$old) { 306 $ret .= ' ago'; 307 } 308 return $this->output($ret, $return); 309 } 310 /** 311 * Alias for timeAgoInWords 312 * 313 * @param string $date_string Datetime string or Unix timestamp 314 * @param string $format Default format if timestamp is used in $date_string 315 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 316 * @return string Relative time string. 317 * @see Time::timeAgoInWords() 318 * @access public 319 */ 320 function relativeTime($datetime_string, $format = 'j/n/y', $return = false) { 321 $date = strtotime($datetime_string); 322 323 if (strtotime("now") > $date) { 324 $ret = $this->timeAgoInWords($datetime_string, $format, false); 325 } else { 326 $ret = $this->timeAgoInWords($datetime_string, $format, true); 327 } 328 329 return $this->output($ret, $return); 330 } 331 /** 332 * Returns true if specified datetime was within the interval specified, else false. 333 * 334 * @param mixed $timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute. 335 * @param mixed $date_string the datestring or unix timestamp to compare 336 * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT. 337 * @return boolean 338 * @access public 339 */ 340 function wasWithinLast($timeInterval, $date_string, $return = false) { 341 $date = $this->fromString($date_string); 342 $result = preg_split('/\\s/', $timeInterval); 343 $numInterval = $result[0]; 344 $textInterval = $result[1]; 345 $currentTime = floor(time()); 346 $seconds = ($currentTime - floor($date)); 347 348 switch($textInterval) { 349 case "seconds": 350 case "second": 351 $timePeriod = $seconds; 352 $ret = $return; 353 break; 354 case "minutes": 355 case "minute": 356 $minutes = floor($seconds / 60); 357 $timePeriod = $minutes; 358 break; 359 case "hours": 360 case "hour": 361 $hours = floor($seconds / 3600); 362 $timePeriod = $hours; 363 break; 364 case "days": 365 case "day": 366 $days = floor($seconds / 86400); 367 $timePeriod = $days; 368 break; 369 case "weeks": 370 case "week": 371 $weeks = floor($seconds / 604800); 372 $timePeriod = $weeks; 373 break; 374 case "months": 375 case "month": 376 $months = floor($seconds / 2629743.83); 377 $timePeriod = $months; 378 break; 379 case "years": 380 case "year": 381 $years = floor($seconds / 31556926); 382 $timePeriod = $years; 383 break; 384 default: 385 $days = floor($seconds / 86400); 386 $timePeriod = $days; 387 break; 388 } 389 if ($timePeriod <= $numInterval) { 390 $ret = true; 391 } else { 392 $ret = false; 393 } 394 return $this->output($ret, $return); 395 } 396 } 397 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |