PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Utility.php

https://github.com/liquidhot/md-master-server
PHP | 67 lines | 51 code | 9 blank | 7 comment | 14 complexity | 0893e707f7143dffbc1b70d23abaa371 MD5 | raw file
  1. <?php
  2. require_once "PasswordHash.php";
  3. require_once "Vars.php";
  4. class Utility {
  5. public static function getSQLConnection() {
  6. if($mysqli == null) {
  7. $mysqli = new mysqli(Config::dbserver,Config::dbusername,Config::dbpassword,Config::dbname);
  8. if ($mysqli->connect_errno) {
  9. trigger_error($mysqli->error,E_USER_ERROR);
  10. }
  11. }
  12. return $mysqli;
  13. }
  14. public static function getPasswordHasher() {
  15. return $hasher = new PasswordHash(Config::passwordRounds, FALSE);
  16. }
  17. /**
  18. * Interval formatting, will use the two biggest interval parts.
  19. * On small intervals, you get minutes and seconds.
  20. * On big intervals, you get months and days.
  21. * Only the two biggest parts are used.
  22. */
  23. public static function formatDateDiff($interval) {
  24. $doPlural = function($nb,$str){return $nb>1?$str.'s':$str;}; // adds plurals
  25. $format = array();
  26. if($interval->y !== 0) {
  27. $format[] = "%y ".$doPlural($interval->y, "year");
  28. }
  29. if($interval->m !== 0) {
  30. $format[] = "%m ".$doPlural($interval->m, "month");
  31. }
  32. if($interval->d !== 0) {
  33. $format[] = "%d ".$doPlural($interval->d, "day");
  34. }
  35. if($interval->h !== 0) {
  36. $format[] = "%h ".$doPlural($interval->h, "hour");
  37. }
  38. if($interval->i !== 0) {
  39. $format[] = "%i ".$doPlural($interval->i, "minute");
  40. }
  41. if($interval->s !== 0) {
  42. if(!count($format)) {
  43. return $interval->format("%s ".$doPlural($interval->s, "second"));
  44. } else {
  45. $format[] = "%s ".$doPlural($interval->s, "second");
  46. }
  47. }
  48. // Use the two largest
  49. if(count($format) > 1) {
  50. $format = array_shift($format)." and ".array_shift($format);
  51. } elseif(count($format) == 0) {
  52. return "less than a second";
  53. } else {
  54. $format = array_pop($format);
  55. }
  56. return $interval->format($format);
  57. }
  58. }