Guest User

Untitled

a guest
Jan 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. <?php
  2.  
  3. //$postedDateTime = '2012-09-23 09:11:02';
  4. $postedDateTime = date('2012-09-23 09:11:02');
  5.  
  6.  
  7. function time_elapsed_since ($postedDateTime){
  8.  
  9. $time = time() - $postedDateTime; // to get the time since that moment
  10.  
  11. $tokens = array (
  12. 31536000 => 'year',
  13. 2592000 => 'month',
  14. 604800 => 'week',
  15. 86400 => 'day',
  16. 3600 => 'hour',
  17. 60 => 'minute',
  18. 1 => 'second'
  19. );
  20.  
  21. foreach ($tokens as $unit => $text) {
  22. if ($time < $unit) continue;
  23. $numberOfUnits = floor($time / $unit);
  24. return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
  25. }
  26.  
  27. }
  28.  
  29.  
  30. echo 'Mr. Tharanga added his tutor profile ' . time_elapsed_since($postedDateTime) . ' ago';
  31.  
  32.  
  33. ?>
  34.  
  35. $postedDateTime = date('2012-09-23 09:11:02');
  36.  
  37. $postedDateTime = strtotime('2012-09-23 09:11:02');
  38.  
  39. $postedDateTime = '2012-09-23 09:11:02';
  40. //$postedDateTime = date('2012-09-23 09:11:02');
  41.  
  42.  
  43. function time_elapsed_since($postedDateTime){
  44. $time = time() - strtotime($postedDateTime); // to get the time since that moment
  45. $tokens = array (
  46. 31536000 => 'year',
  47. 2592000 => 'month',
  48. 604800 => 'week',
  49. 86400 => 'day',
  50. 3600 => 'hour',
  51. 60 => 'minute',
  52. 1 => 'second'
  53. );
  54.  
  55. foreach ($tokens as $unit => $text) {
  56. if ($time < $unit) continue;
  57. $numberOfUnits = floor($time / $unit);
  58. return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
  59. }
  60. }
  61. echo 'Mr. Tharanga added his tutor profile ' . time_elapsed_since($postedDateTime) . ' ago';
  62.  
  63. Try This
  64.  
  65. <?php
  66. //$postedDateTime = '2012-09-23 09:11:02';
  67. date_default_timezone_set('Asia/Kolkata');
  68. $postedDateTime = '2012-09-23 16:48:00';
  69.  
  70.  
  71. function time_elapsed_since($postedDateTime)
  72. {
  73. $start_date = new DateTime($postedDateTime);
  74. $time = $start_date->diff(new DateTime(date('Y-m-d H:i:s'))); // to get the time since that moment
  75. $timeDiff = null ;
  76.  
  77. $timeDiff .= ($time->y) >= 1 ? $time->y .' years' : null;
  78. $timeDiff .= ($time->m) >= 1 ? $time->m .' months' : null;
  79. $timeDiff .= ($time->d) >= 1 ? $time->d .' days' : null;
  80. $timeDiff .= ($time->h) >= 1 ? $time->h .' hours' : null;
  81. $timeDiff .= ($time->i) >= 1 ? $time->i .' minutes' : null;
  82. $timeDiff .= ($time->s) >= 1 ? $time->s .' seconds' : null;
  83.  
  84. return $timeDiff;
  85. }
  86.  
  87. echo 'Mr. Tharanga added his tutor profile ' . time_elapsed_since($postedDateTime) . ' ago';
  88.  
  89. ?>
Add Comment
Please, Sign In to add comment