php计算两个时间差的类

#1 小双

class seday{

function daysInSpan($start,$end){
$dayTicks=$this->ticksInDay();
return ($end-$start)/$dayTicks;
}

function ticksInDay(){
$today=getdate();
$yesterday=mktime(0,0,0,$today[mon],$today[mday]-1,$today[year]);
$today=mktime(0,0,0,$today[mon],$today[mday],$today[year]);
return $today-$yesterday;
}

function day($dayone,$daytwo){
$start_time_array=explode("/",$dayone);
$endtime_time_array=explode("/",$daytwo);

$start_time_year=$start_time_array[0];
$start_time_month=$start_time_array[1];
$start_time_day=$start_time_array[2];

$end_time_year=$endtime_time_array[0];
$end_time_month=$endtime_time_array[1];
$end_time_day=$endtime_time_array[2];

return $this->daysInSpan(mktime(0,0,0,$start_time_month,$start_time_day,0+$start_time_year),
mktime(0,0,0,$end_time_month,$end_time_day,0+$end_time_year)
);
}

}


调用方法:
import("seday.php");
$seday = new seday;
$dayCount = $seday->day("2009/02/23","2009/04/23");
dump($dayCount);

2010-07-28 09:12:23

#2 jake

计算日期差?

其实使用PHP的时间戳,可以轻易的计算出时间差。另外strtotime等日期时间函数也可以很方便的计算。
http://www.php.net/manual/en/function.strtotime.php
比如说

    $date1 = strtotime(2010-10-10);
    $date2 = strtotime(2011-12-01);
    echo ($date2 - $date1)/(24 * 60 * 60); // date2比date1多多少天

2010-07-28 09:46:37