PHP obtains the year, month, day, hour, minute, and second of the specified time period.-php Tutorial

Source: Internet
Author: User
PHP needs to obtain the year, month, day, hour, minute, and second between the specified time period: the front end transmits two standard time formats, such as 2009-05-1212:30, then, return the representation of different units in this time period as needed. I didn't paste the code here for TIME format validation, therefore, you need to add ClassUtils {*** formatMySQLDateTime (yyy php obtains the year, month, day, hour, minute, and second between the specified time periods.
Requirement: the front end transmits two standard time formats, in the format of 12:12:30, and then returns the representation of different units in this time period as needed
I didn't paste the code here for TIME format validation, so I should consider adding
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.