BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time.class.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. #declare(strict_types=1);
  3. if (!extension_loaded('date')) {
  4. error('Date extension not loaded.');
  5. }
  6. function time_ago($TimeStamp)
  7. {
  8. if (!$TimeStamp) {
  9. return false;
  10. }
  11. if (!is_number($TimeStamp)) { // Assume that $TimeStamp is SQL timestamp
  12. $TimeStamp = strtotime($TimeStamp);
  13. }
  14. return time() - $TimeStamp;
  15. }
  16. /*
  17. * Returns a <span> by default but can optionally return the raw time
  18. * difference in text (e.g. "16 hours and 28 minutes", "1 day, 18 hours").
  19. */
  20. function time_diff($TimeStamp, $Levels = 2, $Span = true, $Lowercase = false)
  21. {
  22. if (!$TimeStamp) {
  23. return 'Never';
  24. }
  25. if (!is_number($TimeStamp)) { // Assume that $TimeStamp is SQL timestamp
  26. $TimeStamp = strtotime($TimeStamp);
  27. }
  28. $Time = time() - $TimeStamp;
  29. // If the time is negative, then it expires in the future.
  30. if ($Time < 0) {
  31. $Time = -$Time;
  32. $HideAgo = true;
  33. }
  34. $Years = floor($Time / 31556926); // seconds in one year
  35. $Remain = $Time - $Years * 31556926;
  36. $Months = floor($Remain / 2629744); // seconds in one month
  37. $Remain = $Remain - $Months * 2629744;
  38. $Weeks = floor($Remain / 604800); // seconds in one week
  39. $Remain = $Remain - $Weeks * 604800;
  40. $Days = floor($Remain / 86400); // seconds in one day
  41. $Remain = $Remain - $Days * 86400;
  42. $Hours=floor($Remain / 3600); // seconds in one hour
  43. $Remain = $Remain - $Hours * 3600;
  44. $Minutes = floor($Remain / 60); // seconds in one minute
  45. $Remain = $Remain - $Minutes * 60;
  46. $Seconds = $Remain;
  47. $Return = '';
  48. if ($Years > 0 && $Levels > 0) {
  49. $Return .= "$Years year".(($Years > 1) ? 's' : '');
  50. $Levels--;
  51. }
  52. if ($Months > 0 && $Levels > 0) {
  53. $Return .= ($Return != '') ? ', ' : '';
  54. $Return .= "$Months month".(($Months > 1) ? 's' : '');
  55. $Levels--;
  56. }
  57. if ($Weeks > 0 && $Levels > 0) {
  58. $Return .= ($Return != '') ? ', ' : '';
  59. $Return .= "$Weeks week".(($Weeks > 1) ? 's' : '');
  60. $Levels--;
  61. }
  62. if ($Days > 0 && $Levels > 0) {
  63. $Return .= ($Return != '') ? ', ' : '';
  64. $Return .= "$Days day".(($Days > 1) ? 's' : '');
  65. $Levels--;
  66. }
  67. if ($Hours > 0 && $Levels > 0) {
  68. $Return .= ($Return != '') ? ', ' : '';
  69. $Return .= "$Hours hour".(($Hours > 1) ? 's' : '');
  70. $Levels--;
  71. }
  72. if ($Minutes > 0 && $Levels > 0) {
  73. $Return .= ($Return != '') ? ' and ' : '';
  74. $Return .= "$Minutes min".(($Minutes > 1) ? 's' : '');
  75. }
  76. if ($Return == '') {
  77. $Return = 'Just now';
  78. } elseif (!isset($HideAgo)) {
  79. $Return .= ' ago';
  80. }
  81. if ($Lowercase) {
  82. $Return = strtolower($Return);
  83. }
  84. if ($Span) {
  85. return '<span class="time tooltip" title="'.date('M d Y, H:i', $TimeStamp).'">'.$Return.'</span>';
  86. } else {
  87. return $Return;
  88. }
  89. }
  90. /* SQL utility functions */
  91. function time_plus($Offset)
  92. {
  93. return date('Y-m-d H:i:s', time() + $Offset);
  94. }
  95. function time_minus($Offset, $Fuzzy = false)
  96. {
  97. if ($Fuzzy) {
  98. return date('Y-m-d 00:00:00', time() - $Offset);
  99. } else {
  100. return date('Y-m-d H:i:s', time() - $Offset);
  101. }
  102. }
  103. // This is never used anywhere with $timestamp set
  104. // todo: Why don't we just use NOW() in the sql queries?
  105. function sqltime($timestamp = null)
  106. {
  107. return date('Y-m-d H:i:s', ($timestamp ?? time()));
  108. }
  109. function validDate($DateString)
  110. {
  111. $DateTime = explode(' ', $DateString);
  112. if (count($DateTime) != 2) {
  113. return false;
  114. }
  115. list($Date, $Time) = $DateTime;
  116. $SplitTime = explode(':', $Time);
  117. if (count($SplitTime) != 3) {
  118. return false;
  119. }
  120. list($H, $M, $S) = $SplitTime;
  121. if ($H != 0 && !(is_number($H) && $H < 24 && $H >= 0)) {
  122. return false;
  123. }
  124. if ($M != 0 && !(is_number($M) && $M < 60 && $M >= 0)) {
  125. return false;
  126. }
  127. if ($S != 0 && !(is_number($S) && $S < 60 && $S >= 0)) {
  128. return false;
  129. }
  130. $SplitDate = explode('-', $Date);
  131. if (count($SplitDate) != 3) {
  132. return false;
  133. }
  134. list($Y, $M, $D) = $SplitDate;
  135. return checkDate($M, $D, $Y);
  136. }
  137. function is_valid_date($Date)
  138. {
  139. return is_valid_datetime($Date, 'Y-m-d');
  140. }
  141. function is_valid_time($Time)
  142. {
  143. return is_valid_datetime($Time, 'H:i');
  144. }
  145. function is_valid_datetime($DateTime, $Format = 'Y-m-d H:i')
  146. {
  147. $FormattedDateTime = DateTime::createFromFormat($Format, $DateTime);
  148. return $FormattedDateTime && $FormattedDateTime->format($Format) == $DateTime;
  149. }