PHP obtains the year, month, day, hour, minute, second, and php values between the specified time periods.
Core code:
Class Utils {/*** format MySQL DateTime (YYYY-MM-DD hh: mm: ss) convert the data format found in mysql to the time in seconds * @ param string $ datetime */public function fmDatetime ($ datetime) {$ year = substr ($ datetime ); $ month = substr ($ datetime, 5, 2); $ day = substr ($ datetime, 8, 2); $ hour = substr ($ datetime, 11, 2 ); $ min = substr ($ datetime, 14,2); $ sec = substr ($ datetime, 17,2); return mktime ($ hour, $ min, $ sec, $ month, $ day, 0 + $ year);}/***** obtain the year, month, day, hour, and minute of the two time periods, seconds * @ param String $ start * @ param String $ end * @ return ArrayObject */private function diffDateTime ($ DateStart, $ DateEnd) {$ rs = array (); $ sYear = substr ($ DateStart,); $ eYear = substr ($ DateEnd,); $ sMonth = substr ($ DateStart, 5, 2 ); $ eMonth = substr ($ DateEnd, 5, 2); $ sDay = substr ($ DateStart, 8, 2); $ eDay = substr ($ DateEnd, 8, 2 ); $ startTime = $ this-> fmDatetime ($ DateStart); $ endTime = $ this-> fmDatetime ($ DateEnd); $ dis = $ endTime-$ startTime; // get the seconds of two times $ d = ceil ($ dis/(24*60*60); // get the days $ rs ['day'] = $ d; // days $ rs ['hour'] = ceil ($ dis/(60*60 )); // hour $ rs ['minute '] = ceil ($ dis/60); // minute $ rs ['second'] = $ dis; // seconds $ rs ['Week '] = ceil ($ d/7); // week $ tem = ($ eYear-$ sYear) * 12; // month $ tem1 = $ eYear-$ sYear; // year if ($ eMonth-$ sMonth <0) {// The month minus the negative value $ tem + = ($ eMonth-$ sMonth);} else if ($ eMonth = $ sMonth) {// the same month if ($ eDay-$ sDay >=0) {$ tem ++; $ tem1 ++ ;}} else if ($ eMonth-$ sMonth> 0) {// The month minus plus and minus $ tem1 ++; if ($ eDay-$ sDay> = 0) {// and the date Subtraction is positive $ tem + = ($ eMonth-$ sMonth) + 1;} else {$ tem + = ($ eMonth-$ sMonth );}} $ rs ['month'] = $ tem; $ rs ['Year'] = $ tem1; return $ rs ;}}
One day in a year, two years are returned, and two months are returned for one day in a month ...... I started to look for such an example on the Internet when the project was needed. However, everyone calculated the year as 365 days, and the month as 30 days, the calculated result is definitely useless. The year may be 366 days, and the month may be, 29, or 28.