- /**
- * Function: php time and date tool
- * Edit: bbs.it-home.org
- */
- DateTimeUtils: addDate ('2017-12-01 ', 1, 'y ');
- DateTimeUtils: getWeekDay ('2014/1/01 ','/');
- DateTimeUtils: isLeapYear ('20140901 ');
- DateTimeUtils: timeFromNow (strtotime ("14:15:13 "));
- Class DateTimeUtils {
- /**
- * Checks for leap year, returns true if it is. No 2-digit year check. Also
- * Handles julian calendar correctly.
- * @ Param integer $ year to check
- * @ Return boolean true if is leap year
- */
- Public static function isLeapYear ($ year)
- {
- $ Year = self: digitCheck ($ year );
- If ($ year % 4! = 0)
- Return false;
-
- If ($ year % 400 = 0)
- Return true;
- // If gregorian calendar (> 1582), century not-pisible by 400 is not leap
- Else if ($ year> 1582 & $ year % 100 = 0)
- Return false;
- Return true;
- }
-
- /**
- * Fix 2-digit years. Works for any century.
- * Assumes that if 2-digit is more than 30 years in future, then previous century.
- * @ Param integer $ y year
- * @ Return integer change two digit year into multiple digits
- */
- Protected static function digitCheck ($ y)
- {
- If ($ y <100 ){
- $ Yr = (integer) date ("Y ");
- $ Century = (integer) ($ yr/100 );
-
- If ($ yr % 100> 50 ){
- $ C1 = $ century + 1;
- $ C0 = $ century;
- } Else {
- $ C1 = $ century;
- $ C0 = $ century-1;
- }
- $ C1 * = 100;
- // If 2-digit year is less than 30 years in future, set it to this century
- // Otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
- If ($ y + $ c1) <$ yr + 30) $ y = $ y + $ c1;
- Else $ y = $ y + $ c0 * 100;
- }
- Return $ y;
- }
-
- /**
- * Returns 4-digit representation of the year.
- * @ Param integer $ y year
- * @ Return integer 4-digit representation of the year
- */
- Public static function get4DigitYear ($ y)
- {
- Return self: digitCheck ($ y );
- }
- /**
- * Checks to see if the year, month, day are valid combination.
- * @ Param integer $ y year
- * @ Param integer $ m month
- * @ Param integer $ d day
- * @ Return boolean true if valid date, semantic check only.
- */
- Public static function isValidDate ($ y, $ m, $ d)
- {
- Return checkdate ($ m, $ d, $ y );
- }
-
- Public static function checkDate ($ date, $ separator = "-") {// check whether the date is valid
- $ DateArr = explode ($ separator, $ date );
- Return self: isValidDate ($ dateArr [0], $ dateArr [1], $ dateArr [2]);
- }
- /**
- * Checks to see if the hour, minute and second are valid.
- * @ Param integer $ h hour
- * @ Param integer $ m minute
- * @ Param integer $ s second
- * @ Param boolean $ hs24 whether the hours shocould be 0 through 23 (default) or 1 through 12.
- * @ Return boolean true if valid date, semantic check only.
- * @ Since 1.0.5
- */
- Public static function isValidTime ($ h, $ m, $ s, $ hs24 = true)
- {
- If ($ hs24 & ($ h <0 | $ h> 23) |! $ Hs24 & ($ h <1 | $ h> 12) return false;
- If ($ m> 59 | $ m <0) return false;
- If ($ s> 59 | $ s <0) return false;
- Return true;
- }
-
- Public static function checkTime ($ time, $ separator = ":") {// check whether the time is valid
- $ TimeArr = explode ($ separator, $ time );
- Return self: isValidTime ($ timeArr [0], $ timeArr [1], $ timeArr [2]);
- }
-
- Public static function addDate ($ date, $ int, $ unit = "d") {// increase of date
- $ Value = array ('y' => '', 'M' =>'', 'D' => '');
- $ DateArr = explode ("-", $ date );
- If (array_key_exists ($ unit, $ value )){
- $ Value [$ unit] = $ int;
- } Else {
- Return false;
- }
- Return date ("Y-m-d", mktime (0, 0, 0, $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);
- }
-
- Public static function addDateTime ($ date, $ int, $ unit = "d") {// increase of date
- $ Value = array ('y' => '', 'M' =>'', 'D' => '', 'H' => '', 'I' => '');
- $ DateArr = preg_split ("/-| \ s |:/", $ date );
- If (array_key_exists ($ unit, $ value )){
- $ Value [$ unit] = $ int;
- } Else {
- Return false;
- }
- 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']);
- }
-
- Public static function addDayTimestamp ($ ntime, $ aday) {// obtains the number of days after the current time. the unit of the number of days is 1.
- $ Dayst = 3600*24;
- $ Oktime = $ ntime + ($ aday * $ dayst );
- Return $ oktime;
- }
-
- Public static function dateDiff ($ begin, $ end, $ unit = "d") {// Time comparison function, returns the seconds, minutes, hours, or days of difference between two dates
- $ Diff = strtotime ($ end)-strtotime ($ begin );
- Switch ($ unit)
- {
- Case "y": $ retval = bcp ($ diff, (60*60*24*365); break;
- Case "m": $ retval = bcp ($ diff, (60*60*24*30); break;
- Case "w": $ retval = bcp ($ diff, (60*60*24*7); break;
- Case "d": $ retval = bcp ($ diff, (60*60*24); break;
- Case "h": $ retval = bcp ($ diff, (60*60); break;
- Case "I": $ retval = bcp ($ diff, 60); break;
- Case "s": $ retval = $ diff; break;
- }
- Return $ retval;
- }
-
- Public static function getWeekDay ($ date, $ separator = "-") {// calculate the day of the week
- $ DateArr = explode ($ separator, $ date );
- Return date ("w", mktime (0, 0, 0, $ dateArr [1], $ dateArr [2], $ dateArr [0]);
- }
-
- Public static function timeFromNow ($ dateline) {// display the date as: XX days before XX years
- If (empty ($ dateline) return false;
- $ Seconds = time ()-$ dateline;
- If ($ seconds <60 ){
- Return "1 minute ago ";
- } Elseif ($ seconds <3600 ){
- Return floor ($ seconds/60). "minutes ago ";
- } Elseif ($ seconds <24*3600 ){
- Return floor ($ seconds/3600). "hours ago ";
- } Elseif ($ seconds <48*3600 ){
- Return date ("yesterday H: I", $ dateline )."";
- } Else {
- Return date ('Y-m-D', $ dateline );
- }
- }
-
- Public static function transDateToChs ($ date ){
- If (empty ($ date) return 'Today ';
- Date_default_timezone_set ('prc ');
- $ Dates = date ('y, m, dday', strtotime ($ date ));
- Return $ dates;
- }
-
- // 08/31/2004 => 2004-08-31
- Public static function TransDateUI ($ datestr, $ type = 'Y-m-D '){
- If ($ datestr = Null)
- Return Null;
- $ Target = $ datestr;
- $ Arr_date = preg_split ("//", $ target );
- $ Monthstr = $ arr_date [0];
- $ Daystr = $ arr_date [1];
- $ Yearstr = $ arr_date [2];
- $ Result = date ($ type, mktime (0, 0, 0, $ monthstr, $ daystr, $ yearstr ));
- Return $ result;
- }
-
- // 12/20/2004 10:55:00 AM =>
- Public static function TransDateTimeUI ($ datestr, $ type = 'Y-m-d H: I: s '){
- If ($ datestr = Null)
- Return Null;
- $ Target = $ datestr;
- $ Arr_date = preg_split ("// | \ s |:/", $ target );
- $ Monthstr = $ arr_date [0];
- $ Daystr = $ arr_date [1];
- $ Yearstr = $ arr_date [2];
- $ Hourstr = $ arr_date [3];
- $ Minutesstr = $ arr_date [4];
- $ Result = date ($ type, mktime ($ hourstr, $ minutesstr, 0, $ monthstr, $ daystr, $ yearstr ));
- Return $ result;
- }
- }
- ?>
|