PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.0RC1/src/LibWebta/library/Data/Formater/class.Formater.php

http://scalr.googlecode.com/
PHP | 349 lines | 161 code | 40 blank | 148 comment | 66 complexity | fc0cf4ebfc1510f94d1489c429b34e2b MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. /**
  3. * This file is a part of LibWebta, PHP class library.
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 2 of the GPL license,
  8. * that is bundled with this package in the file license.txt and is
  9. * available through the world-wide-web at the following url:
  10. * http://www.gnu.org/copyleft/gpl.html
  11. *
  12. * @category LibWebta
  13. * @package Data
  14. * @subpackage Formater
  15. * @copyright Copyright (c) 2003-2007 Webta Inc, http://www.gnu.org/licenses/gpl.html
  16. * @license http://www.gnu.org/licenses/gpl.html
  17. *
  18. */
  19. /**
  20. * @name Formater
  21. * @category LibWebta
  22. * @package Data
  23. * @subpackage Formater
  24. * @version 1.0
  25. * @author Igor Savchenko <http://webta.net/company.html>
  26. */
  27. class Formater extends Core
  28. {
  29. /**
  30. * Convert bytes to readable string
  31. *
  32. * @static
  33. * @param int $bytes
  34. * @return string
  35. */
  36. static function Bytes2String($bytes)
  37. {
  38. if (!$bytes)
  39. $bytes = 0;
  40. if ($bytes < 1024)
  41. return "{$bytes} bytes";
  42. elseif ($bytes >= 1024 && $bytes < 1024*1024)
  43. return round($bytes/1024, 2)." KB";
  44. elseif ($bytes >= 1024*1024 && $bytes < 1024*1024*1024)
  45. return round($bytes/1024/1024, 2)." MB";
  46. else
  47. return round($bytes/1024/1024/1024, 2)." GB";
  48. }
  49. /**
  50. * Fuzzinate a string
  51. *
  52. * @static
  53. * @return timestamp
  54. * @param string $string
  55. */
  56. static function Date2Fuzzy($string)
  57. {
  58. $time = strtotime($string);
  59. return (self::FuzzyTimeString($time));
  60. }
  61. /**
  62. * Compares two dates.
  63. *
  64. * Returns:
  65. *
  66. * < 0 if date1 is less than date2;
  67. * > 0 if date1 is greater than date2;
  68. * 0 if they are equal.
  69. *
  70. * @static
  71. * @return int
  72. * @param string|timestamp $date1
  73. * @param string|timestamp $date2
  74. */
  75. static function CompareDates($date1, $date2)
  76. {
  77. if (!is_numeric($date1)) {
  78. $date1 = self::TimeString2Stamp($date1);
  79. }
  80. if (!is_numeric($date2)) {
  81. $date2 = self::TimeString2Stamp($date2);
  82. }
  83. if ($date1 < $date2) {
  84. return -1;
  85. } else if ($date1 > $date2) {
  86. return 1;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. /**
  92. * Converts a date/time string to Unix timestamp
  93. *
  94. * @static
  95. * @return timestamp
  96. * @param string $string
  97. */
  98. static function TimeString2Stamp($string)
  99. {
  100. return strtotime($string);
  101. }
  102. /**
  103. * Converts Unix timestamp to a date/time string using format given
  104. *
  105. * Special options can be passed for the format parameter. These are
  106. * set format types. The options currently include:
  107. *
  108. * o mysql
  109. *
  110. * If the time parameter isn't supplied, then the current local time
  111. * will be used.
  112. *
  113. * @static
  114. * @return string
  115. * @param integer $time
  116. * @param string $format
  117. */
  118. static function TimeStamp2String($time = 0, $format = 'Y-m-d H:i:s')
  119. {
  120. if ($format == 'mysql') {
  121. $format = 'Y-m-d H:i:s';
  122. }
  123. if ($time == 0) {
  124. $time = time();
  125. }
  126. return date($format, $time);
  127. }
  128. /**
  129. * Converts a Unix timestamp or date/time string to a specific format.
  130. *
  131. * Special options can be passed for the format parameter. These are
  132. * set format types. The options currently include:
  133. *
  134. * o mysql
  135. *
  136. * If the time parameter isn't supplied, then the current local time
  137. * will be used.
  138. *
  139. * @static
  140. * @return string
  141. * @param integer|string $time
  142. * @param string $format
  143. * @see TimeString2Stamp()
  144. * @see TimeStamp2String()
  145. */
  146. static function TimeFormat($time = 0, $format = 'Y-m-d H:i:s')
  147. {
  148. if (!is_numeric($time)) {
  149. $time = self::TimeStringToStamp($time);
  150. }
  151. if ($time == 0) {
  152. $time = time();
  153. }
  154. return self::TimeStamp2String($time, $format);
  155. }
  156. /**
  157. * Converts a Unix timestamp or date/time string to a human-readable
  158. * format, such as '1 day, 2 hours, 42 mins, and 52 secs'
  159. *
  160. * Based on the word_time() function from PG+ (http://pgplus.ewtoo.org)
  161. *
  162. * @static
  163. * @return string
  164. * @param integer|string $time
  165. * @see TimeString2Stamp()
  166. */
  167. static function Time2HumanReadable($time = 0)
  168. {
  169. if (!is_numeric($time)) {
  170. $time = self::TimeString2Stamp($time);
  171. }
  172. if ($time == 0) {
  173. return 'Unknown';
  174. } else {
  175. if ($time < 0) {
  176. $neg = 1;
  177. $time = 0 - $time;
  178. } else {
  179. $neg = 0;
  180. }
  181. $days = $time / 86400;
  182. $days = floor($days);
  183. $hrs = ($time / 3600) % 24;
  184. $mins = ($time / 60) % 60;
  185. $secs = $time % 60;
  186. $timestring = '';
  187. if ($neg) {
  188. $timestring .= 'negative ';
  189. }
  190. if ($days) {
  191. $timestring .= "$days day" . ($days == 1 ? '' : 's');
  192. if ($hrs || $mins || $secs) {
  193. $timestring .= ', ';
  194. }
  195. }
  196. if ($hrs) {
  197. $timestring .= "$hrs hour" . ($hrs == 1 ? '' : 's');
  198. if ($mins && $secs) {
  199. $timestring .= ', ';
  200. }
  201. if (($mins && !$secs) || (!$mins && $secs)) {
  202. $timestring .= ' and ';
  203. }
  204. }
  205. if ($mins) {
  206. $timestring .= "$mins min" . ($mins == 1 ? '' : 's');
  207. if ($mins && $secs) {
  208. $timestring .= ', ';
  209. }
  210. if ($secs) {
  211. $timestring .= ' and ';
  212. }
  213. }
  214. if ($secs) {
  215. $timestring .= "$secs sec" . ($secs == 1 ? '' : 's');
  216. }
  217. return $timestring;
  218. }
  219. }
  220. /**
  221. * Give a slightly more fuzzy time string. such as: yesterday at 3:51pm
  222. *
  223. *
  224. * @static
  225. * @return string
  226. * @param integer|string $time
  227. * @param integer $tz_offset timezone offset
  228. * @see TimeString2Stamp()
  229. */
  230. static function FuzzyTimeString($time = 0, $tz_offset = null)
  231. {
  232. $time = self::CorrectTime($time, $tz_offset);
  233. $now = self::CorrectTime(0, $tz_offset);
  234. $sodTime = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
  235. $sodNow = mktime(0, 0, 0, date('m', $now), date('d', $now), date('Y', $now));
  236. if ($sodNow == $sodTime) {
  237. return 'today at ' . date('g:ia', $time); // check 'today'
  238. } else if (($sodNow - $sodTime) <= 86400) {
  239. return 'yesterday at ' . date('g:ia', $time); // check 'yesterday'
  240. } else if (($sodNow - $sodTime) <= 432000) {
  241. return date('l \a\\t g:ia', $time); // give a day name if within the last 5 days
  242. } else if (date('Y', $now) == date('Y', $time)) {
  243. return date('M j \a\\t g:ia', $time); // miss off the year if it's this year
  244. } else {
  245. return date('M j, Y \a\\t g:ia', $time); // return the date as normal
  246. }
  247. }
  248. /**
  249. * Correct time with current timezone offset
  250. *
  251. * @static
  252. * @param integer time time to convert
  253. * @param float tz_offset timezone offset in hours
  254. *
  255. * @return integer time
  256. */
  257. public static function CorrectTime($time = 0, $tz_offset = null)
  258. {
  259. if (!is_numeric($time))
  260. $time = self::TimeString2Stamp($time);
  261. if (!$time)
  262. $time = time();
  263. return (is_null($tz_offset) ? $time : $time - date('Z') + $tz_offset * 3600);
  264. }
  265. /**
  266. * Method for calculating time past till now
  267. *
  268. * @static
  269. * @param integer timestamp time
  270. * @param float tz_offset timezone offset in hours
  271. * @param integer maxdepth depth for time items (years, months, ...)
  272. * @param string suffix suffix for formatted string
  273. *
  274. * @return string formatted result string
  275. */
  276. public static function TimeAgo($timestamp = 0, $tz_offset = null, $maxdepth = 2, $suffix = "ago", $needtz = true)
  277. {
  278. if (!$timestamp) return "never";
  279. $timestamp = self::CorrectTime($timestamp, $needtz ? $tz_offset : null);
  280. // Store the current time
  281. $current_time = self::CorrectTime(0, $tz_offset);
  282. // Determine the difference, between the time now and the timestamp
  283. $difference = $current_time - $timestamp;
  284. // Set the periods of time
  285. $periods = array("second", "min", "hr", "day", "week", "month", "year", "decade");
  286. // Set the number of seconds per period
  287. $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
  288. // Determine which period we should use, based on the number of seconds lapsed.
  289. // If the difference divided by the seconds is more than 1, we use that. Eg 1 year / 1 decade = 0.1, so we move on
  290. // Go from decades backwards to seconds
  291. for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
  292. // Ensure the script has found a match
  293. if ($val < 0) $val = 0;
  294. // Determine the minor value, to recurse through
  295. $new_time = $current_time - ($difference % $lengths[$val]);
  296. // Set the current value to be floored
  297. $number = floor($number);
  298. // If required create a plural
  299. if($number != 1) $periods[$val].= "s";
  300. // Return text
  301. $text = sprintf("%d %s ", $number, $periods[$val]);
  302. // Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
  303. if (($val >= 1) && (($current_time - $new_time) > 0) && ($maxdepth - 1 > 0)){
  304. $text .= self::TimeAgo($new_time, $tz_offset, --$maxdepth, "", false);
  305. }
  306. return $text . $suffix;
  307. }
  308. }
  309. ?>