Home > Backend Development > PHP Tutorial > PHP calculates the difference between two times (seconds, minutes, hours, days, months, years)_PHP Tutorial

PHP calculates the difference between two times (seconds, minutes, hours, days, months, years)_PHP Tutorial

WBOY
Release: 2016-07-13 10:19:01
Original
1041 people have browsed it

PHP calculates the difference between two times (seconds, minutes, hours, days, months, years)

Example code for the month difference between two times:

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"));

 代码如下  

$yourdate="2012-10-20";
$yourdate_unix=strtotime($yourdate);
echo (date("Y",$yourdate_unix)-date("Y"))*12+(date("m",$yourdate_unix)-date("m"));

Example 1

The code is as follows

/*
* Calculate the month difference between two time periods
* @param $st start time $et end time (timestamp format)
* @return $total the difference returned
*/
function getMonthNum($st, $et)
{
$s_m = date('n', $st);
$e_m = date('n', $et);
$s_y = date('Y', $st);
$e_y = date('Y', $et);
$total = 13 - $s_m + ($e_y - $s_y - 1) * 12 + $e_m; //Calculate month difference
Return $total;
}

 代码如下  

/*
    * 计算2个时间段的月份差
 * @param $st开始时间 $et结束时间(时间戳格式)
 * @return $total 返回的差值 
   */
   function getMonthNum($st, $et)
   {
    $s_m = date('n', $st);
    $e_m = date('n', $et);
    $s_y = date('Y', $st);
    $e_y = date('Y', $et);
    $total = 13 - $s_m + ($e_y - $s_y - 1) * 12 + $e_m; //计算月份差
    return $total;
   }

  例子2

 代码如下  

$one = strtotime('2011-05-08 07:02:40');//开始时间 时间戳
$tow = strtotime('2012-12-25 00:00:00');//结束时间 时间戳
$cle = $tow - $one; //得出时间戳差值

代码如下

$one = strtotime('2011-05-08 07:02:40');//开始时间 时间戳
$tow = strtotime('2012-12-25 00:00:00');//结束时间 时间戳
$cle = $tow - $one; //得出时间戳差值

/* 这个只是提示
echo ceil($cle/60); //得出一共多少分钟
echo ceil($cle/3600); //得出一共多少小时
echo ceil($cle/3600/24); //得出一共多少天
*/
/*ceil()函数,即进一法取整*/
$d = cell($cle/3600/24);
$h = cell(($cle%(3600*24))/3600); //%取余
$m = cell(($cle%(3600*24))/60);

echo "两个时间相差 $d 天 $h 小时 $m 分"
?>

/* 这个只是提示
echo ceil($cle/60); //得出一共多少分钟
echo ceil($cle/3600); //得出一共多少小时
echo ceil($cle/3600/24); //得出一共多少天
*/
/*ceil()函数,即进一法取整*/
$d = cell($cle/3600/24);
$h = cell(($cle%(3600*24))/3600);  //%取余
$m = cell(($cle%(3600*24))/60);

echo "两个时间相差 $d 天 $h 小时 $m 分"
?>

 代码如下  

/*
*
*函数功能:计算两个以YYYY-MM-DD为格式的日期,相差几天
*
*/
function getChaBetweenTwoDate($date1,$date2){

$Date_List_a1=explode("-",$date1);
$Date_List_a2=explode("-",$date2);

$d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);

$d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);

$Days=round(($d1-$d2)/3600/24);

return $Days;
}

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

  例子3
 代码如下  

/*
*
*函数功能:计算两个以YYYY-MM-DD为格式的日期,相差几天
*
*/
function getChaBetweenTwoDate($date1,$date2){<🎜>

    $Date_List_a1=explode("-",$date1);
    $Date_List_a2=explode("-",$date2);<🎜>

    $d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);<🎜>

    $d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);<🎜>

    $Days=round(($d1-$d2)/3600/24);<🎜>

    return $Days;
}<🎜>

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

  例子4

 代码如下  

$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.”天
”;
echo $hour.”小时
”;
echo $minute.”分钟
”;
echo $second.”秒
”;
?>

 代码如下  

$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.”天
”;
echo $hour.”小时
”;
echo $minute.”分钟
”;
echo $second.”秒
”;
?>

  例子四是我最喜欢的一个可以计算到天小时秒哦,当然具体的还是需要根据自己的需要了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/878456.htmlTechArticlePHP计算两个时间的差(秒 分 时 天 月 年) 两个时间之间月份差实例代码: 代码如下 $yourdate=2012-10-20; $yourdate_unix=strtotime($yourdate); echo (date(Y...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template