Php time and date tool implementation code

Source: Internet
Author: User
Php time and date tool implementation code

  1. /**
  2. * Function: php time and date tool
  3. * Edit: bbs.it-home.org
  4. */
  5. DateTimeUtils: addDate ('2017-12-01 ', 1, 'y ');
  6. DateTimeUtils: getWeekDay ('2014/1/01 ','/');
  7. DateTimeUtils: isLeapYear ('20140901 ');
  8. DateTimeUtils: timeFromNow (strtotime ("14:15:13 "));
  9. Class DateTimeUtils {
  10. /**
  11. * Checks for leap year, returns true if it is. No 2-digit year check. Also
  12. * Handles julian calendar correctly.
  13. * @ Param integer $ year to check
  14. * @ Return boolean true if is leap year
  15. */
  16. Public static function isLeapYear ($ year)
  17. {
  18. $ Year = self: digitCheck ($ year );
  19. If ($ year % 4! = 0)
  20. Return false;
  21. If ($ year % 400 = 0)
  22. Return true;
  23. // If gregorian calendar (> 1582), century not-pisible by 400 is not leap
  24. Else if ($ year> 1582 & $ year % 100 = 0)
  25. Return false;
  26. Return true;
  27. }
  28. /**
  29. * Fix 2-digit years. Works for any century.
  30. * Assumes that if 2-digit is more than 30 years in future, then previous century.
  31. * @ Param integer $ y year
  32. * @ Return integer change two digit year into multiple digits
  33. */
  34. Protected static function digitCheck ($ y)
  35. {
  36. If ($ y <100 ){
  37. $ Yr = (integer) date ("Y ");
  38. $ Century = (integer) ($ yr/100 );
  39. If ($ yr % 100> 50 ){
  40. $ C1 = $ century + 1;
  41. $ C0 = $ century;
  42. } Else {
  43. $ C1 = $ century;
  44. $ C0 = $ century-1;
  45. }
  46. $ C1 * = 100;
  47. // If 2-digit year is less than 30 years in future, set it to this century
  48. // Otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  49. If ($ y + $ c1) <$ yr + 30) $ y = $ y + $ c1;
  50. Else $ y = $ y + $ c0 * 100;
  51. }
  52. Return $ y;
  53. }
  54. /**
  55. * Returns 4-digit representation of the year.
  56. * @ Param integer $ y year
  57. * @ Return integer 4-digit representation of the year
  58. */
  59. Public static function get4DigitYear ($ y)
  60. {
  61. Return self: digitCheck ($ y );
  62. }
  63. /**
  64. * Checks to see if the year, month, day are valid combination.
  65. * @ Param integer $ y year
  66. * @ Param integer $ m month
  67. * @ Param integer $ d day
  68. * @ Return boolean true if valid date, semantic check only.
  69. */
  70. Public static function isValidDate ($ y, $ m, $ d)
  71. {
  72. Return checkdate ($ m, $ d, $ y );
  73. }
  74. Public static function checkDate ($ date, $ separator = "-") {// check whether the date is valid
  75. $ DateArr = explode ($ separator, $ date );
  76. Return self: isValidDate ($ dateArr [0], $ dateArr [1], $ dateArr [2]);
  77. }
  78. /**
  79. * Checks to see if the hour, minute and second are valid.
  80. * @ Param integer $ h hour
  81. * @ Param integer $ m minute
  82. * @ Param integer $ s second
  83. * @ Param boolean $ hs24 whether the hours shocould be 0 through 23 (default) or 1 through 12.
  84. * @ Return boolean true if valid date, semantic check only.
  85. * @ Since 1.0.5
  86. */
  87. Public static function isValidTime ($ h, $ m, $ s, $ hs24 = true)
  88. {
  89. If ($ hs24 & ($ h <0 | $ h> 23) |! $ Hs24 & ($ h <1 | $ h> 12) return false;
  90. If ($ m> 59 | $ m <0) return false;
  91. If ($ s> 59 | $ s <0) return false;
  92. Return true;
  93. }
  94. Public static function checkTime ($ time, $ separator = ":") {// check whether the time is valid
  95. $ TimeArr = explode ($ separator, $ time );
  96. Return self: isValidTime ($ timeArr [0], $ timeArr [1], $ timeArr [2]);
  97. }
  98. Public static function addDate ($ date, $ int, $ unit = "d") {// increase of date
  99. $ Value = array ('y' => '', 'M' =>'', 'D' => '');
  100. $ DateArr = explode ("-", $ date );
  101. If (array_key_exists ($ unit, $ value )){
  102. $ Value [$ unit] = $ int;
  103. } Else {
  104. Return false;
  105. }
  106. Return date ("Y-m-d", mktime (0, 0, 0, $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);
  107. }
  108. Public static function addDateTime ($ date, $ int, $ unit = "d") {// increase of date
  109. $ Value = array ('y' => '', 'M' =>'', 'D' => '', 'H' => '', 'I' => '');
  110. $ DateArr = preg_split ("/-| \ s |:/", $ date );
  111. If (array_key_exists ($ unit, $ value )){
  112. $ Value [$ unit] = $ int;
  113. } Else {
  114. Return false;
  115. }
  116. Return date ("Y-m-d H: I: s", mktime ($ dateArr [3] + $ value ['H'], $ dateArr [4] + $ value ['I'], $ dateArr [5], $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);
  117. }
  118. Public static function addDayTimestamp ($ ntime, $ aday) {// obtains the number of days after the current time. the unit of the number of days is 1.
  119. $ Dayst = 3600*24;
  120. $ Oktime = $ ntime + ($ aday * $ dayst );
  121. Return $ oktime;
  122. }
  123. Public static function dateDiff ($ begin, $ end, $ unit = "d") {// Time comparison function, returns the seconds, minutes, hours, or days of difference between two dates
  124. $ Diff = strtotime ($ end)-strtotime ($ begin );
  125. Switch ($ unit)
  126. {
  127. Case "y": $ retval = bcp ($ diff, (60*60*24*365); break;
  128. Case "m": $ retval = bcp ($ diff, (60*60*24*30); break;
  129. Case "w": $ retval = bcp ($ diff, (60*60*24*7); break;
  130. Case "d": $ retval = bcp ($ diff, (60*60*24); break;
  131. Case "h": $ retval = bcp ($ diff, (60*60); break;
  132. Case "I": $ retval = bcp ($ diff, 60); break;
  133. Case "s": $ retval = $ diff; break;
  134. }
  135. Return $ retval;
  136. }
  137. Public static function getWeekDay ($ date, $ separator = "-") {// calculate the day of the week
  138. $ DateArr = explode ($ separator, $ date );
  139. Return date ("w", mktime (0, 0, 0, $ dateArr [1], $ dateArr [2], $ dateArr [0]);
  140. }
  141. Public static function timeFromNow ($ dateline) {// display the date as: XX days before XX years
  142. If (empty ($ dateline) return false;
  143. $ Seconds = time ()-$ dateline;
  144. If ($ seconds <60 ){
  145. Return "1 minute ago ";
  146. } Elseif ($ seconds <3600 ){
  147. Return floor ($ seconds/60). "minutes ago ";
  148. } Elseif ($ seconds <24*3600 ){
  149. Return floor ($ seconds/3600). "hours ago ";
  150. } Elseif ($ seconds <48*3600 ){
  151. Return date ("yesterday H: I", $ dateline )."";
  152. } Else {
  153. Return date ('Y-m-D', $ dateline );
  154. }
  155. }
  156. Public static function transDateToChs ($ date ){
  157. If (empty ($ date) return 'Today ';
  158. Date_default_timezone_set ('prc ');
  159. $ Dates = date ('y, m, dday', strtotime ($ date ));
  160. Return $ dates;
  161. }
  162. // 08/31/2004 => 2004-08-31
  163. Public static function TransDateUI ($ datestr, $ type = 'Y-m-D '){
  164. If ($ datestr = Null)
  165. Return Null;
  166. $ Target = $ datestr;
  167. $ Arr_date = preg_split ("//", $ target );
  168. $ Monthstr = $ arr_date [0];
  169. $ Daystr = $ arr_date [1];
  170. $ Yearstr = $ arr_date [2];
  171. $ Result = date ($ type, mktime (0, 0, 0, $ monthstr, $ daystr, $ yearstr ));
  172. Return $ result;
  173. }
  174. // 12/20/2004 10:55:00 AM =>
  175. Public static function TransDateTimeUI ($ datestr, $ type = 'Y-m-d H: I: s '){
  176. If ($ datestr = Null)
  177. Return Null;
  178. $ Target = $ datestr;
  179. $ Arr_date = preg_split ("// | \ s |:/", $ target );
  180. $ Monthstr = $ arr_date [0];
  181. $ Daystr = $ arr_date [1];
  182. $ Yearstr = $ arr_date [2];
  183. $ Hourstr = $ arr_date [3];
  184. $ Minutesstr = $ arr_date [4];
  185. $ Result = date ($ type, mktime ($ hourstr, $ minutesstr, 0, $ monthstr, $ daystr, $ yearstr ));
  186. Return $ result;
  187. }
  188. }
  189. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.