How PHP gets the year, month, day, hour, minute, and second code instance between the specified time period

Source: Internet
Author: User
The front end passes two standard time format come over, format like 2009-05-12 12:12:30, then return according to need different units of this time period of representation, for time format of check I here code is not posted out, so use time oneself consider add

Core code:

Class Utils {/** * format mysql DateTime (yyyy-mm-dd hh:mm:ss) converts the data format found in MySQL into time seconds * @param string $datetime */pu  Blic function Fmdatetime ($datetime) {$year = substr ($datetime, 0,4);  $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);} /** * * * * * * * * * * * * * * * * * for two time inclusive years, months, days, hours, minutes, seconds * @param string $start * @param string $end * @return Arrayobject */Private F Unction diffdatetime ($DateStart, $DateEnd) {$rs = array (); $sYear = substr ($DateStart, 0,4); $eYear = substr ($DateEnd, 0,4) ; $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 two time seconds $d = Ceil ($dis/(24*60*60));//Get Days $rs[' Day ' = $d;//Days $rs[' hour '] = Ceil ($dis/(60*60)) ;//Hours $rs[' minute ']= Ceil ($dis/60);//min $rs[' second '] = $dis;//Seconds $rs[' Week '] = ceil ($d/7);//week $tem = ($eYear-$sYear) *12;//Month $tem1 = $eYear-$ syear;//if ($eMonth-$sMonth <0) {//month subtracts negative $tem + = ($eMonth-$sMonth);} else if ($eMonth = = $sMonth) {//month same if ($eDay-$sDay >=0) {$tem + +; $tem 1++;}} else if ($eMonth-$sMonth >0) {//month minus $tem1++;if ($eDay-$sDay >=0) {//and date subtracted to positive $tem + = ($eMonth-$sMonth) +1;} else{$tem + = ($eMonth-$sMonth);}} $rs [' month '] = $tem; $rs [' year '] = $tem 1;return $rs;}}

More than a year, the return is 2 years, one months more than a day to return is 2 months, to push ... Project needs, just do this out, I also go to the Internet to find such examples, but everyone is the year on the basis of 365 days to calculate, the month according to 30 days to calculate, so that the results are certainly useless, the year may be 366 days, the month may be 31,29,28 are likely to

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.