PHP calculates the difference of two times (second time of Day of the month year) _php tutorial

Source: Internet
Author: User

PHP calculates the difference of two times (second time of day of the month)


The month difference between two time instance codes:

The code is as follows

$yourdate = "2012-10-20";
$yourdate _unix=strtotime ($yourdate);
Echo (Date ("Y", $yourdate _unix)-date ("Y")) *12+ (date ("M", $yourdate _unix)-date ("M"));

Example 1

code as follows

/*
* Calculates the month difference for 2 time periods
* @param $st start time $et end time (timestamp format)
* @return $total return Difference
*/
function Getmonthnum ($st, $et)
{
$s _m = date (' n ', $st),
$e _m = Date (' n ', $et),
$s _y = date (' y ', $st);
$e _y = d Ate (' Y ', $et);
$total = 13-$s _m + ($e _y-$s _y-1) * + $e _m;//Calculate month difference
return $total;
}

Example 2

The code is as follows

$one = Strtotime (' 2011-05-08 07:02:40 ');//Start time timestamp
$tow = Strtotime (' 2012-12-25 00:00:00 ');//End time Timestamp
$cle = $tow-$one; The time stamp difference value is obtained

/* This is just a hint
echo ceil ($cle/60); How many minutes.
echo ceil ($cle/3600); How many hours?
Echo ceil ($cle/3600/24); How many days
*/
/*ceil () function, that is, into a method to take the whole * *
$d = cell ($cle/3600/24);
$h = cell (($cle% (3600*24))/3600); % take-up
$m = cell (($cle% (3600*24))/60);

echo "Two time difference $d days $h hours $m minutes"
?>

Example 3

The code is as follows

/*
*
* Function function: Calculates two dates in YYYY-MM-DD format, a few days apart
*
*/
function Getchabetweentwodate ($date 1, $date 2) {

$Date _list_a1=explode ("-", $date 1);
$Date _list_a2=explode ("-", $date 2);

$d 1=mktime (0,0,0, $Date _list_a1[1], $Date _list_a1[2], $Date _list_a1[0]);

$d 2=mktime (0,0,0, $Date _list_a2[1], $Date _list_a2[2], $Date _list_a2[0]);

$Days =round (($d 1-$d 2)/3600/24);

return $Days;
}

echo getchabetweentwodate (' 2010-08-11 ', ' 2010-08-16 ');
echo "
";
echo getchabetweentwodate (' 2010-08-16 ', ' 2010-08-11 ');
?>

Example 4

code as follows

$startdate = "2010-12-11 11:40:00″;
$enddate = "2012-12-12 11:45:09″;
$date =floor ((Strtotime ($enddate)-strtotime ($startdate))/86400);
$hour =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400/3600);
$minute =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400/60);
$second =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400%60);
echo $date. " Days
”;
echo $hour. " Hours
”;
echo $minute. " Minutes
”;
echo $second. " Seconds
”;
?>

Example four is my favorite one can be calculated to days and hours of the second Oh, of course, the specific need to be based on their own needs.

http://www.bkjia.com/PHPjc/878456.html www.bkjia.com true http://www.bkjia.com/PHPjc/878456.html techarticle PHP calculates the difference between two time (s) and two time of month difference instance code: The code is as follows $yourdate =2012-10-20; $yourdate _unix=strtotime ($yourdate); Echo (the date (Y. ..

  • 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.