Php time and date tool class & lt ;? PhpDateTimeUtils: addDate (2012-12-01,1, y); DateTimeUtils: getWeekDay (20121001,); DateTimeUtils: isLeapYear (2012 ); classDateTimeUtils {*** Check php time and date tool class
1582), century not-pisible by 400 is not leapelse 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;}/*** Retu Rns 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); if (is_numeric ($ dateArr [0]) & is_numeric ($ dateArr [1]) & is_numeric ($ dateArr [2]) {return checkdate ($ dateArr [1], $ dateArr [2], $ dateArr [0]);} return false;}/*** Checks to see if the hour, minute and seco Nd are valid. * @ param integer $ h hour * @ param integer $ m minute * @ param integer $ s second * @ param boolean $ hs24 whether the hours shoshould 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); if (is_numeric ($ timeArr [0]) & is_numeric ($ timeArr [1]) & is_numeric ($ timeArr [2]) {if ($ timeArr [0]> = 0 & $ timeArr [0] <= 23) & ($ timeArr [1]> = 0 & $ timeArr [1] <= 59) & ($ timeArr [2]> = 0 & $ timeArr [2] <= 59) return true; elsereturn false;} return false;} public static function addDate ($ date, $ int, $ unit = "d ") {// add a date $ dateArr = explode ("-", $ date); $ value [$ unit] = $ int; return date ("Y-m-d ", mktime (0, 0, 0, $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);} public static function addDayTim Estamp ($ ntime, $ aday) {// obtains the number of days after the current time. the unit for increasing the number of days is 1 $ dayst = 3600*24; $ oktime = $ ntime + ($ aday * $ dayst); return $ oktime;} public static function dateDiff ($ date1, $ date2, $ unit = "d ") {// Time comparison function, returns the seconds, minutes, hours, or days between two dates switch ($ unit) {case's ': $ pidend = 1; break; case 'I': $ pidend = 60; break; case 'H': $ pidend = 3600; break; case 'D': $ pidend = 86400; break; default: $ pidend = 86400 ;}$ time1 = strtotime ($ date1 ); $ Time2 = strtotime ($ date2); if ($ time1 & $ time2) return (float) ($ time1-$ time2)/$ pidend; return false ;} // 2011-12-06 => public static function getWeekDay ($ date, $ separator = "-") {// calculate the given date as 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 floorTime ($ seconds) {// display the date as: XX days before XX $ times = ''; $ da Ys = floor ($ seconds/86400) % 30); $ hours = floor ($ seconds/3600) % 24); $ minutes = floor ($ seconds/60) % 60); $ seconds = floor ($ seconds % 60); if ($ seconds> = 1) $ times. = $ seconds. 'second'; if ($ minutes> = 1) $ times = $ minutes. 'Min '. $ times; if ($ hours> = 1) $ times = $ hours. 'Hourly '. $ times; if ($ days> = 1) $ times = $ days. 'day'; if ($ days> 30) return false; $ times. = 'pre'; return str_replace ("", '', $ Times);} public static function transDateToChs ($ date) {if (empty ($ date) return 'day '; $ y = date ('Y', strtotime ($ date); $ m = date ('M', strtotime ($ date )); $ d = date ('D', strtotime ($ date); return $ y. 'year '. $ m. 'month '. $ d. 'day';} // 08/31/2004 => 2004-08-31public static function TransDateUI ($ datestr, $ type = 'Y-m-D') {if ($ datestr = Null) return Null; $ target = $ datestr; $ ar R_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 AM => 2004-12-20 10: 55: 00 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;} // 2004-08-31 => 08/31/2004 public static function TransDateDB ($ datestr, $ type = 'M/d/Y') {if ($ datestr = Null) return Null; if ($ datestr =' Between -00-00 ') return Null; $ target = $ datestr; $ arr_date = preg_split ("/-/", $ target); $ monthstr = $ arr_date [1]; $ daystr = $ arr_date [2]; $ yearstr = $ arr_date [0]; $ result = date ($ type, mktime (0, 0, 0, $ monthstr, $ daystr, $ yearstr); return $ result;} // 10:55:00 => 12/20/2004 AM public static function TransDateTimeDB ($ datestr, $ type = 'M/d/Y h: I a') {if ($ datestr = Null) return Nu Ll; $ target = $ datestr; $ arr_date = preg_split ("/-| \ s |:/", $ target); $ monthstr = $ arr_date [1]; $ daystr = $ arr_date [2]; $ yearstr = $ arr_date [0]; $ hourstr = $ arr_date [3]; $ minutesstr = $ arr_date [4]; $ secondstr = $ arr_date [5]; $ result = date ($ type, mktime ($ hourstr, $ minutesstr, $ secondstr, $ monthstr, $ daystr, $ yearstr )); return $ result ;}?>?