PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/www/protected/pages/utils/DateUtils.php

http://smartbook.googlecode.com/
PHP | 81 lines | 60 code | 8 blank | 13 comment | 16 complexity | e4c234591db84ec9d7aab5ac3219bbe3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * Created on 16 ??.?. 2009
  4. *
  5. * To change the template for this generated file go to
  6. * Window - Preferences - PHPeclipse - PHP - Code Templates
  7. */
  8. class DateUtils
  9. {
  10. public static function process_datetime($date)
  11. {
  12. $dateNow = date("Y/m/d H:i:s");
  13. // echo "<br /> dateNow " . $dateNow;
  14. // echo "<br /> dateToBeDiff " . $date;
  15. $sec = strtotime($dateNow) - strtotime($date);
  16. // echo "\nnum secs : " . $sec;
  17. if($sec < 0)
  18. return "Error ";
  19. else if($sec < 60)
  20. return "now";
  21. else if($sec < 60*60) // 1 minute - 59 minute
  22. return DateUtils::getMinutesDiff($sec);
  23. else if($sec < 60*60*24) // 1 hr - 23 hr
  24. return DateUtils::getHoursDiff($sec);
  25. else if($sec < 60*60*24*30) // 1 day - 29 day
  26. return DateUtils::getDayDiff($sec);
  27. else if($sec < 60*60*24*30*12) // 1 month - 11 months
  28. return DateUtils::getMonthDiff($sec);
  29. else
  30. return DateUtils::getYearDiff($sec);
  31. }
  32. public static function getMinutesDiff($sec)
  33. { // number of sec in a minute
  34. return ceil( $sec / 60) . " minutes ago ";
  35. }
  36. public static function getHoursDiff($sec)
  37. {
  38. // number of sec in an hour
  39. $hour = floor( $sec / (60*60));
  40. if($hour <= 1)
  41. return "an hour ago ";
  42. else
  43. return $hour. " hours ago ";
  44. }
  45. public static function getDayDiff($sec)
  46. {
  47. // number of sec in a day
  48. $day = floor( $sec / (60*60*24));
  49. if($day <= 1)
  50. return "a day ago ";
  51. else
  52. return $day. " days ago ";
  53. }
  54. public static function getMonthDiff($sec)
  55. {
  56. // number of sec in a month
  57. $month = floor( $sec / (60*60*24*30));
  58. if($month <= 1)
  59. return "a month ago ";
  60. else
  61. return $month. " months ago ";
  62. }
  63. public static function getYearDiff($sec)
  64. {
  65. // number of sec in a year
  66. $year = floor( $sec / (60*60*24*30*12));
  67. if($year <= 1)
  68. return "a month ago ";
  69. else
  70. return $year. " years ago ";
  71. }
  72. }
  73. ?>