Guest User

Untitled

a guest
Jan 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. function get_date_format($strToTimeString){
  2.  
  3. $minute = 60;
  4. $hour = $minutes * 60;
  5. $day = $hour * 24;
  6. $week = $day * 7;
  7. $month = $week * 4;
  8. $year = $month * 12;
  9.  
  10. $timeNow = strtotime("now");
  11.  
  12. $timeDiff = $timeNow - $strToTimeString;
  13.  
  14. if($timeDiff > $minute){
  15. if($timeDiff > $hour){
  16. if($timeDiff > $day){
  17. if($timeDiff > $week){
  18. if($timeDiff > $month){
  19. if($timeDiff > $year){
  20. // Years ago
  21. }
  22. else{
  23. // Months ago
  24. }
  25. }
  26. else{
  27. // Weeks ago
  28. }
  29. }
  30. else{
  31. // Days ago
  32. }
  33. }
  34. else
  35. {
  36. // Hours ago
  37. }
  38. }
  39. else{
  40. // Minutes ago
  41. }
  42.  
  43. }
  44. else{
  45. // Seconds ago
  46. }
  47.  
  48.  
  49. }
  50.  
  51. function returnInterval($date){
  52. $datetime1 = new DateTime($date);
  53. $datetime2 = new DateTime();
  54. $diff = $datetime1->diff($datetime2);
  55. $string = '';
  56. $pass = '';
  57.  
  58. if($diff->y){
  59. $string .= ($diff->y == 1) ? $diff->y." year" : $diff->y." years";
  60. $pass = ', ';
  61. }
  62. if($diff->m){
  63. $string .= $pass;
  64. $string .= ($diff->m == 1) ? $diff->m." month" : $diff->m." months";
  65. $pass = ', ';
  66. }
  67. if($diff->d){
  68. $string .= $pass;
  69. $string .= ($diff->d == 1) ? $diff->d." day" : $diff->d." days";
  70. $pass = ', ';
  71. }
  72. if($diff->h){
  73. $string .= $pass;
  74. $string .= ($diff->h == 1) ? $diff->h." hour" : $diff->h." hours";
  75. $pass = ', ';
  76. }
  77. if($diff->i){
  78. $string .= $pass;
  79. $string .= ($diff->i == 1) ? $diff->i." minute" : $diff->i." minutes";
  80. $pass = ', ';
  81. }
  82. if($diff->s){
  83. $string .= $pass;
  84. $string .= ($diff->s == 1) ? $diff->s." second" : $diff->s." seconds";
  85. }
  86. $pos = strrpos($string, ',');
  87. $string = substr_replace($string, ' and ', $pos, 2);
  88. return $string;
  89. }
  90.  
  91. echo returnInterval('2012-09-19 13:28:45');
  92. // 8 days, 13 hours, 47 minutes and 44 seconds
  93.  
  94. $now = new DateTime('now');
  95. $posted = new DateTime($postDateFromDBHere);
  96. $interval = $posted->diff($now);
  97. var_dump($interval);
  98. echo $interval->format('%y-%m-%d %h:%m:%s'); //You can do similar to format your output as you wish.
  99.  
  100. function time_ago($date)
  101. {
  102. //echo "ss";
  103. if (empty($date)) {
  104. return "No date provided";
  105. }
  106. $periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
  107. $lengths = array("60","60","24","7","4.35","12","10");
  108. $now = time();
  109. $unix_date = strtotime($date);
  110. // check validity of date
  111. if (empty($unix_date)) {
  112. return "Bad date";
  113. }
  114. // is it future date or past date
  115. if ($now >= $unix_date) {
  116. $difference= $now - $unix_date;
  117. $tense = "ago";
  118. } else {
  119. $difference = $unix_date - $now;
  120. $tense = "from now";
  121. }
  122. for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  123. $difference /= $lengths[$j];
  124. }
  125. $difference = round($difference);
  126.  
  127. if ($difference != 1 && $j != 0) {
  128. $periods[$j].= "s";
  129. }
  130. if($difference!=0)
  131. return "$difference $periods[$j] {$tense}";
  132. else
  133. return "a few seconds ago";
  134. }
  135.  
  136. function time_elapsed_since ($postedDateTime){
  137.  
  138. $time = time() - $postedDateTime; // to get the time since that moment
  139.  
  140. $tokens = array (
  141. 31536000 => 'year',
  142. 2592000 => 'month',
  143. 604800 => 'week',
  144. 86400 => 'day',
  145. 3600 => 'hour',
  146. 60 => 'minute',
  147. 1 => 'second'
  148. );
  149.  
  150. foreach ($tokens as $unit => $text) {
  151. if ($time < $unit) continue;
  152. $numberOfUnits = floor($time / $unit);
  153. return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
  154. }
  155.  
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. time_elapsed_since($postedDateTime).' ago';
  163.  
  164. $diff = time() - $previousStamp;
  165. // diff is the difference between now, and when the event happened.
Add Comment
Please, Sign In to add comment