PHP Days Ago Script


/ Published in: PHP
Save to your folder(s)

Working on creating a 'days ago' script. I know some already exist, but I just want to know how to do it myself. I'm also incorporating leap year functionality and some utility functions that could be used on other projects.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. //=====================================================
  4. // REFERENCE
  5. //=====================================================
  6. //60 Seconds
  7. // 1 minute = 60 seconds
  8. // 1 month =
  9. // 31 days = 2678400
  10. // 30 days = 2592000
  11. // 28 days = 2419200
  12. // 1 hour = 3600 seconds
  13. // 1 day = 86400 seconds
  14. // 1 week = 604800 seconds
  15. // 1 year = 31536000 seconds
  16. //=====================================================
  17.  
  18. echo displayago();
  19.  
  20. function displayago()
  21. {
  22.  
  23. $datePublished = mktime(11, 33, 0, 5, 2, 2009);
  24. echo 'Date Published: ' . date('l F j, Y ', $datePublished) . ' at '. strftime('%H:%M%p', $datePublished) . '<br/>';
  25.  
  26. $dateNow = getdate();
  27. $dateNow = mktime($dateNow['hours'], $dateNow['minutes'], $dateNow['seconds'], $dateNow['mon'], $dateNow['mday'], $dateNow['year']);
  28. echo 'Todays Date: ' . date('l F j, Y ', $dateNow) . ' at '. strftime('%H:%M%p', $dateNow) . '<br/>';
  29. echo '<br/>';
  30.  
  31. $totalSeconds = $dateNow - $datePublished;
  32.  
  33. $yearSeconds = 31536000; //31536000 seconds in a year
  34. //$monthSeconds = '';
  35. $weekSeconds = 604800; // 604800 seconds in a week
  36. $daySeconds = 86400; // 86400 seconds in a day
  37. $hourSeconds = 3600; //3600 seconds in a minute
  38. $minuteSeconds = 60; //60 seconds
  39. $seconds = 1;
  40.  
  41. $result = '';
  42.  
  43. //calculate total leap years
  44. $totalLeapYears = totalLeapYears(date('Y', $datePublished),$dateNow['year']);
  45.  
  46. //calculate years
  47. $years = round($totalSeconds/$yearSeconds);
  48. if($years >= 1)
  49. {
  50. if($years == 1)
  51. {
  52. $result .= $years . ' <b>year</b> ';
  53. }
  54. else
  55. {
  56. $result .= $years . ' <b>years</b> ';
  57. }
  58. }
  59. else
  60. {
  61. $result .= '0 <b>years</b> ';
  62. }
  63.  
  64. //calculate months
  65. $months = round();
  66.  
  67. //calculate weeks
  68.  
  69. //calculate days
  70. $days = round($totalSeconds/$daySeconds);
  71. if($days >= 365)
  72. {
  73. $days = $days - 365;
  74. }
  75. if($days >= 1)
  76. {
  77. if(round($days) == 1)
  78. {
  79. $result .= round($days) . ' <b>day</b> ';
  80. }
  81. else
  82. {
  83. $result .= round($days) . ' <b>days</b> ';
  84. }
  85. }
  86. else
  87. {
  88. $result .= '0 <b>days</b> ';
  89. }
  90.  
  91. //calculate hours
  92. $hours = round($totalSeconds/$hourSeconds);
  93. if($hours >= 1)
  94. {
  95. if($hours == 1)
  96. {
  97. $result .= $hours . ' <b>hour</b> ';
  98. }
  99. else
  100. {
  101. $result .= $hours . ' <b>hours</b> ';
  102. }
  103. }
  104. else
  105. {
  106. $result .= '0 <b>hours</b> ';
  107. }
  108.  
  109. //calculate minutes
  110. $minutes = round($totalSeconds/$minuteSeconds);
  111. if($minutes >= 1)
  112. {
  113. if($minutes == 1)
  114. {
  115. $result .= $minutes . ' <b>minute</b> ';
  116. }
  117. else
  118. {
  119. $result .= $minutes . ' <b>minutes</b> ';
  120. }
  121. }
  122. else
  123. {
  124. $result .= '0 <b>minutes</b> ';
  125. }
  126.  
  127. //calculate seconds
  128. $seconds = $totalSeconds/$seconds;
  129. if($seconds >= 1)
  130. {
  131. if($seconds == 1)
  132. {
  133. $result .= $seconds . ' <b>second</b> ';
  134. }
  135. else
  136. {
  137. $result .= $seconds . ' <b>seconds</b> ';
  138. }
  139. }
  140. else
  141. {
  142. $result .= '0 <b>seconds</b> ';
  143. }
  144.  
  145. $result .= ' ago';
  146.  
  147. return $result;
  148. }
  149.  
  150. //=====================================================================
  151. // UTILITY FUNCTIONS
  152. //=====================================================================
  153. /*
  154. Returns the total amount of years between
  155. a certain amount of years.
  156.  
  157. @return
  158. */
  159. function totalLeapYears($startYear, $endYear)
  160. {
  161. $result = 0;
  162. for($i = $startYear; $i < $endYear; $i++)
  163. {
  164. if(date('L', strtotime("$i-01-01")))
  165. {
  166. $result += 1;
  167. }
  168. }
  169. return $result;
  170. }
  171.  
  172. /*
  173. Returns the total amount of days per year,
  174. checking for leap year
  175.  
  176. @return
  177. */
  178. function getTotalDays($isleapyear = false)
  179. {
  180. if($isleapyear)
  181. {
  182. return 366;
  183. }
  184.  
  185. return 365;
  186. }
  187.  
  188. /*
  189. Returns the total amount of days per month,
  190. checking for leap year as well (February)
  191.  
  192. @return
  193. */
  194. function getDays($month, $isleapyear = false)
  195. {
  196. switch($month)
  197. {
  198. case 'January':
  199. return 31;
  200. break;
  201. case 'February':
  202. if($isleapyear)
  203. {
  204. //is leap year
  205. return 29;
  206. }
  207. else
  208. {
  209. //is not leap year
  210. return 28;
  211. }
  212. break;
  213. case 'March':
  214. return 31;
  215. break;
  216. case 'April':
  217. return 30;
  218. break;
  219. case 'May':
  220. return 31;
  221. break;
  222. case 'June':
  223. return 30;
  224. break;
  225. case 'July':
  226. return 31;
  227. break;
  228. case 'August':
  229. return 31;
  230. break;
  231. case 'September':
  232. return 30;
  233. break;
  234. case 'October':
  235. return 31;
  236. break;
  237. case 'November':
  238. return 30;
  239. break;
  240. case 'December':
  241. return 31;
  242. break;
  243. default:
  244. break;
  245. }
  246. }
  247. //=====================================================================
  248.  
  249. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.