- function count_days($a,$b){
- $a_dt=getdate($a);
- $b_dt=getdate($b);
- $a_new=mktime(12,0,0,$a_dt['mon'],$a_dt['mday'],$a_dt['year']);
- $b_new=mktime(12,0,0,$b_dt['mon'],$b_dt['mday'],$b_dt['year']);
- return round(abs($a_new-$b_new)/86400);
- }
- //今天与2008年10月11日相差多少天
- $date1=strtotime(time());
- $date1=strtotime('10/11/2008');
- $result=count_days($date1,$date2);
- echo $result;
- ?>
方法2:
- //今天与2008年9月9日相差多少天
- $Date_1=date("Y-m-d");
- $Date_2="2008-10-11";
- $d1=strtotime($Date_1);
- $d2=strtotime($Date_2);
- $Days=round(($d2-$d1)/3600/24);
- echo "今天与2008年10月11日相差".$Days."天";
- ?>
PHP获得任意时间与当前时间的时间差
代码:
- #功能:获得任意时间与当前时间的时间差
- function QueryDays($datestr){
- #格式化时间
- $da=preg_split("/(-| |:)/i",$datestr);
- $nowyear=date("Y");
- $nowmon=date("n");
- $nowday=date("d");
- $nowtimes=mktime(0,0,0,$nowmon,$nowday,$nowyear);
- $pdtimes= mktime(0,0,0,$nowmon,$nowday,$nowyear-1);
- $bjtimes= mktime(0,0,0,$da[1],$da[2],$da[0]);
- #判断所给出的时间是不是在一年内
- if ($bjtimes>=$pdtimes and $bjtimes<=$nowtimes){
- return (floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime($da[3],$da[4],$da[5],$da[1],$da[2],$da[0]))));
- }else{
- $loop=$nowyear-$da[0];
- $totaldays=(floor(strftime("%j",mktime(0,0,0,$nowmon,$nowday,$nowyear)-mktime(0,0,0,1,1,$nowyear))));
- for($i=1;$i<=$loop;$i++){
- for($j=12;$j>=1;$j--){
- if ($da[0]==$nowyear-$i and $da[1]==$j){
- $days=MonDays($nowyear-$i,$j);
- return $totaldays+=$days-$da[2];
- break;
- }else{
- $days=MonDays($nowyear-$i,$j);
- $totaldays+=$days;
- }//end else
- }//end for
- }//end for
- }//end else
- }//end function
- #取得月分的天数
- function MonDays($year,$month){
- switch ($month){
- case "1":
- case "3":
- case "5":
- case "7":
- case "8":
- case "10":
- case "12": $days=31;break;
- case "4":
- case "6":
- case "9":
- case "11": $days=30;break;
- case "2":
- if (checkdate($month,29,$year)){
- $days=29;
- }else{
- $days=28;
- }//end else
- break;
- }//end switch
- return $days;
- }//end function
- $datestr="2002-1-14 9:47:20";
- echo QueryDays($datestr);
- ?>
PHP计算两个时间相差的年月时分秒
PHP如何计算两个时间相差几年几月几时几分几秒
先将两个时间转化为时间戳,然后相减得到两个时间相差的秒数,最后进行一些算术就可以得到两个时间相差的年月日时分秒了。
代码:
- $time1 = "2008-6-15 11:49:59"; //第一个时间
- $time2 = "2007-5-5 12:53:28"; //第二个时间
- $t1 = strtotime($time1);
- $t2 = strtotime($time2);
- $t12 = abs($t1-$t2);
- $start = 0;
- $string = "两个时间相差:";
- $y = floor($t12/(3600*24*360));
- if($start || $y )
- {
- $start = 1;
- $t12 -= $y*3600*24*360;
- $string .= $y."年";
- }
- $m = floor($t12/(3600*24*31));
- if($start || $m)
- {
- $start = 1;
- $t12 -= $m*3600*24*31;
- $string .= $m."月";
- }
- $d = floor($t12/(3600*24));
- if($start || $d)
- {
- $start = 1;
- $t12 -= $d*3600*24;
- $string .= $d."天";
- }
- $h = floor($t12/(3600));
- if($start || $h)
- {
- $start = 1;
- $t12 -= $h*3600;
- $string .= $h."时";
- }
- $s = floor($t12/(60));
- if($start || $s)
- {
- $start = 1;
- $t12 -= $s*60;
- $string .= $s."分";
- }
- $string .= "{$t12}秒";
- echo $string;
- ?>
|