PHP gets the _php tips for the year, month, day, time, minute, and second between the specified time periods

Source: Internet
Author: User

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 $datetim
	  E/Public 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); 
	 /** * * * The number of years, months, days, hours, minutes, seconds * @param string $start * @param string $end * @return Arrayobject based on two times
		
		* * Private Function 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///The number of seconds to get two times $d = Ceil ($dis/(24*60*60)//Get the number of days $rs [' Day '] = $d;//Days $rs [' hour '] = Ceil ($dis/(60*60));/hour $rs [' minute '] = ceil ($dis/60);/min $rs [' second '] = $dis;//sec $rs [' Week '] = ceil ($d/7);/Week $tem = ($eYear-$sYear) *12;//month $tem 1 = $eYear-$sYear;//year if ($eMonth-$sMonth <0)
		{//month subtraction is negative $tem + = ($eMonth-$sMonth);
				}else if ($eMonth = = $sMonth) {//month same if ($eDay-$sDay >=0) {$tem + +;
			$tem 1++;
			}else if ($eMonth-$sMonth >0) {//month subtraction positive $tem 1++;
			if ($eDay-$sDay >=0) {//and the date is subtracted to a positive number $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 2 months, to push ... Project needs to do this out, I also went online to find such an example, but everyone is the year on the basis of 365 days, the month on the basis of 30 days to calculate, so that the results are certainly useless, the year may be 366 days, the month may be 31,29,28 may be the

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.