PHP obtains the year, month, day, hour, minute, second, and php values between the specified time periods.

Source: Internet
Author: User

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.

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.