/modules/trivial/dates.php

https://github.com/dionyziz/blogcube · PHP · 166 lines · 138 code · 25 blank · 3 comment · 25 complexity · 73b42f395f8fcdf0e5280e7e9523ca9b MD5 · raw file

  1. <?php
  2. # Developer: dionyziz
  3. include "modules/module.php";
  4. $shortmonths = Array( "Jan" , "Feb" , "Mar" , "Apr" ,
  5. "May" , "Jun" , "Jul" , "Aug" ,
  6. "Sep" , "Oct" , "Nov" , "Dec" );
  7. $months = Array( "January" , "February" , "March" , "April" ,
  8. "May" , "June" , "July" , "August" ,
  9. "September" , "October" , "November" , "December" );
  10. define( 'BCT_BIG_BANG' , '2005-10-25 17:56:29' ); // GMT
  11. define( 'BCT_OFFSET' , 64 ); // BCT years
  12. define( 'BCT_FACTOR' , 4 * PI );
  13. function BCT2GMT( $bct /* :SQL DATETIME */ ) {
  14. $bigbang = strtotime( BCT_BIG_BANG , 0 );
  15. $bcta = explode("-", $bct, 2);
  16. $bct = ($bcta[ 0 ] - BCT_OFFSET) . "-" . $bcta[ 1 ];
  17. $time = strtotime( $bct , 0 );
  18. $gmt = $time;
  19. $gmt -= $bigbang;
  20. $gmt /= BCT_FACTOR;
  21. $gmt += $bigbang;
  22. return date( "Y-m-d H:i:s" , $gmt );
  23. }
  24. function GMT2BCT( $gmt /* :SQL DATETIME */ ) {
  25. $bigbang = strtotime( BCT_BIG_BANG , 0 );
  26. $time = strtotime( $gmt , 0 );
  27. $bct = $time;
  28. $bct -= $bigbang;
  29. $bct *= BCT_FACTOR;
  30. $bct += $bigbang;
  31. $years = date( "Y" , $bct );
  32. $years = $years + BCT_OFFSET;
  33. return $years . date( "-m-d H:i:s" , $bct );
  34. }
  35. function UniqueTimeStamp() {
  36. $Asec = explode( " " , microtime() );
  37. $Amicro = explode( "." , $Asec[ 0 ] );
  38. return $Asec[ 1 ] . substr( $Amicro[ 1 ] , 0 , 4 );
  39. }
  40. function HumanDate( $date ) {
  41. global $shortmonths;
  42. ParseDate( $date , $year , $month , $day , $hour , $minute , $second );
  43. return $day . " " . $shortmonths[ $month - 1 ] . " $year, $hour:$minute:$second";
  44. }
  45. function HumanSolDate( $date ) {
  46. global $shortmonths;
  47. ParseSolDate( $date , $year , $month , $day );
  48. return $day . " " . $shortmonths[ $month - 1 ] . " $year";
  49. }
  50. function ParseDate( $date , &$year , &$month , &$day , &$hour , &$minute , &$second ) {
  51. if ( !$date || $date == "0000-00-00 00:00:00" )
  52. return;
  53. $dateElements = split(' ', $date);
  54. ParseSolDate( $dateElements[ 0 ] , $year , $month , $day );
  55. $dateTimeElements = split(':', $dateElements[ 1 ] );
  56. $hour = $dateTimeElements[ 0 ];
  57. $minute = $dateTimeElements[ 1 ];
  58. $second = $dateTimeElements[ 2 ];
  59. }
  60. function ParseSolDate( $date , &$year , &$month , &$day ) {
  61. if ( !$date || $date == "0000-00-00" )
  62. return;
  63. $dateDateElements = split('-', $date );
  64. $year = $dateDateElements[ 0 ];
  65. $month = $dateDateElements[ 1 ];
  66. $day = $dateDateElements[ 2 ];
  67. }
  68. function NowDate() {
  69. return gmdate("Y-m-d H:i:s", time() );
  70. }
  71. function NowSolDate() {
  72. return gmdate("Y-m-d", time() );
  73. }
  74. function DateDiff( $olddate = "0000-00-00 00:00:00" , $recentdate = "0000-00-00 00:00:00" ) {
  75. // returns the number of seconds elapsed between $olddate and $recentdate, or the number of seconds from $olddate until now, if $recentdate is not passed
  76. if( $olddate == "0000-00-00 00:00:00" )
  77. return false;
  78. if( $recentdate == "0000-00-00 00:00:00" )
  79. $recentdate = NowDate();
  80. $recentdatesec = strtotime( $recentdate );
  81. $olddatesec = strtotime( $olddate );
  82. return $recentdatesec - $olddatesec;
  83. }
  84. function FullDate( $datetime ) {
  85. // $datetime in SQL DATETIME format
  86. return date( "l, F d, Y" , strtotime( $datetime ) );
  87. }
  88. function BCDate( $datetime ) {
  89. $diff = DateDiff( $datetime );
  90. if( $diff < 0 ) {
  91. $diff = -$diff;
  92. }
  93. if( $diff < 60 ) {
  94. return "a few seconds ago";
  95. }
  96. $minute = 60;
  97. $hour = $minute * 60;
  98. if( $diff < $hour ) {
  99. $minutes = Div( $diff , $minute );
  100. if( $minutes < 5 ) {
  101. return "a few minutes ago";
  102. }
  103. if( $minutes < 10 ) {
  104. return "10 minutes ago";
  105. }
  106. if( $minutes < 15 ) {
  107. return "15 minutes ago";
  108. }
  109. if( $minutes < 25 ) {
  110. return "20 minutes ago";
  111. }
  112. if( $minutes < 35 ) {
  113. return "half an hour ago";
  114. }
  115. if( $minutes < 50 ) {
  116. return "45 minutes ago";
  117. }
  118. return "an hour ago";
  119. }
  120. if( $diff < $hour * 23 ) {
  121. $hours = Div( $diff , $hour );
  122. if( $hours == 1 ) {
  123. return "an hour ago";
  124. }
  125. return Div( $diff , $hour ) . " hours ago";
  126. }
  127. $day = $hour * 24;
  128. if( $diff < $day * 2 ) {
  129. return "yesterday";
  130. }
  131. if( $diff < $day * 6 ) {
  132. return Div( $diff , $day ) . " days ago";
  133. }
  134. $week = $day * 7;
  135. if( $diff < $week + $day ) {
  136. return "a week ago";
  137. }
  138. $xdatetime = explode( " " , $datetime );
  139. return HumanSolDate( $xdatetime[ 0 ] );
  140. }
  141. ?>