PHP Time Tools

Source: Internet
Author: User
PHP Time tool class & lt ;? PhpclassDateUtils {functioncheckDate ($ date) {check whether the date is a valid date $ dateArrexplode (-, $ date); if (is_numeric ($ dateArr [0]) & amp; is_numeric ($ PHP Time tool class
 = 0 & $ timeArr [0] <= 23) & ($ timeArr [1]> = 0 & $ timeArr [1] <= 59) & ($ timeArr [2]> = 0 & $ timeArr [2] <= 59) return true; elsereturn false;} return false;} function addDate ($ date, $ int, $ unit = "d") {// increase in time (you can also increase the value by hour, minute, and second, and add the value after time) $ dateArr = explode ("-", $ date); $ value [$ unit] = $ int; return date ("Y-m-d", mktime (0, 0, 0, 0, $ dateArr [1] + $ value ['M'], $ dateArr [2] + $ value ['D'], $ dateArr [0] + $ value ['Y']);} function addDay ($ ntime, $ aday) {// A few days after the current time, the unit for increasing the number of days is 1 $ dayst = 3600*24; $ oktime = $ ntime + ($ aday * $ dayst); return $ oktime;} function dateDiff ($ date1, $ date2, $ unit = "") {// Time comparison function, returns the seconds, minutes, hours, or days of difference between the 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 = strt Otime ($ date1); $ time2 = strtotime ($ date2); if ($ time1 & $ time2) return (float) ($ time1-$ time2)/$ pidend; return false;} function getWeekDay ($ date) {// calculate the given date of the week $ dateArr = explode ("-", $ date ); return date ("w", mktime (0, 0, 0, $ dateArr [1], $ dateArr [2], $ dateArr [0]);} function getDateTimeMk ($ mktime) {// Obtain the current time, in the format of 2009-10-23 14: 20: 35 return MyDate ('Y-m-d H: I: S ', $ mktime);} functio N getDateMk ($ mktime) {// Obtain the current time, in the format of 2009-10-23if ($ mktime = "0") return "no "; elsereturn MyDate ("Y-m-d", $ mktime);} function getMkTime ($ dtime) {// converts the time format to the Linux timestamp global $ cfg_cli_time; if (! Ereg ("[^ 0-9]", $ dtime) {return $ dtime;} $ dtime = trim ($ dtime); $ dt = Array (1970, 1, 1, 0, 0, 0); $ dtime = ereg_replace ("[\ r \ n \ t] | day | second", "", $ dtime ); $ dtime = str_replace ("year", "-", $ dtime); $ dtime = str_replace ("month", "-", $ dtime ); $ dtime = str_replace ("hour", ":", $ dtime); $ dtime = str_replace ("minute", ":", $ dtime ); $ dtime = trim (ereg_replace ("[] {1,}", "", $ dtime); $ ds = explode (" ", $ Dtime); $ ymd = explode ("-", $ ds [0]); if (! Isset ($ ymd [1]) {$ ymd = explode (". ", $ ds [0]);} if (isset ($ ymd [0]) {$ dt [0] = $ ymd [0];} if (isset ($ ymd [1]) {$ dt [1] = $ ymd [1];} if (isset ($ ymd [2]) {$ dt [2] = $ ymd [2];} if (strlen ($ dt [0]) = 2) {$ dt [0] = '20 '. $ dt [0];} if (isset ($ ds [1]) {$ hms = explode (":", $ ds [1]); if (isset ($ hms [0]) {$ dt [3] = $ hms [0];} if (isset ($ hms [1]) {$ dt [4] = $ hms [1];} if (isset ($ Hms [2]) {$ dt [5] = $ hms [2] ;}} foreach ($ dt as $ k => $ v) {$ v = ereg_replace ("^ 0 {1,}", '', trim ($ v); if ($ v = '') {$ dt [$ k] = 0 ;}$ mt = @ gmmktime ($ dt [3], $ dt [4], $ dt [5], $ dt [1], $ dt [2], $ dt [0])-3600 * $ 1__cli_time; if (! Empty ($ mt) {return $ mt;} else {return time () ;}} function MyDate ($ format = 'Y-m-d H: I: S ', $ timest = 0) {// returns Greenwich Mean Time // global $ cfg_cli_time = '+ 8'; global $ cfg_cli_time; $ addtime = $ cfg_cli_time * 3600; if (empty ($ format) {$ format = 'Y-m-d H: I: s';} return gmdate ($ format, $ timest + $ addtime );} function floorTime ($ seconds) {// display the date as: XX days before XX years $ times = ''; $ days = 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);} function transDate ToChs ($ 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-31function 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 AM => 2004-12-20 10: 55: 00 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]; $ yea Rstr = $ 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 function TransDateDB ($ datestr, $ type = 'M/d/Y') {if ($ datestr = Null) return Null; if ($ datestr = '2017-00-00 ') return Null; $ target = $ datestr; $ arr_date = preg_split ("/-/", $ target); $ months Tr = $ arr_date [1]; $ daystr = $ arr_date [2]; $ yearstr = $ arr_date [0]; $ result = date ($ type, mktime (0, 0, 0, 0, $ monthstr, $ daystr, $ yearstr); return $ result;} // 10:55:00 => 12/20/2004 AM function TransDateTimeDB ($ datestr, $ type = 'M/d/Y h: I a') {if ($ datestr = Null) return Null; $ 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 ;}?>
?

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.