note 111618 deleted from function.date-diff by aharvey

From: Date: Fri, 08 Mar 2013 20:19:57 +0000
Subject: note 111618 deleted from function.date-diff by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-194001@lists.php.net to get a copy of this message
Note Submitter: billyabm at hotmail dot com ---- ##Billy functions function countTime($time,$format="%YY years %MM months %DD days %HH hours %mm minutes %ss seconds",$type=1){ ## TYPE #1 seconds #2 minutes #3 hours #4 days switch($type){ case 1 : break; case 2 : $time=$time*60;break; case 3 : $time=$time*3600; break; case 4 : $time=$time*86400; break; } $arr[0] ="/%YY/"; #years $arr[1] ="/%MM/"; #moths $arr[2] ="/%DD/"; #days $arr[3] ="/%HH/"; #hours $arr[4] ="/%mm/"; #minutes $arr[5] ="/%ss/"; #seconds $string=$format; if(preg_match($arr[0],$format)) { $c=$time>=31536000?floor($time/31536000):0; $time=$time>=31536000? $time%31536000 :$time; $string=preg_replace($arr[0],$c,$string);} if(preg_match($arr[1],$format)) { $c=$time>=2592000?floor($time/2592000):0; $time=$time>=2592000? $time%2592000:$time; $string=preg_replace($arr[1],$c,$string);} if(preg_match($arr[2],$format)) { $c=$time>=86400?floor($time/86400):0; $time=$time>=86400? $time%86400: $time; $string=preg_replace($arr[2],$c,$string);} if(preg_match($arr[3],$format)) { $c=$time>=3600?floor($time/3600):0; $time=$time>=3600? $time%3600 :$time; $string=preg_replace($arr[3],$c,$string);} if(preg_match($arr[4],$format)) { $c=$time>=60?floor($time/60):0; $time=$time>=60? $time%60 :$time; $string=preg_replace($arr[4],$c,$string);} if(preg_match($arr[5],$format)) { $c=$time; $string=preg_replace($arr[5],$c,$string);} $string =preg_replace($arr,"",$string); return $string; } function datediff($sdate,$sdate2,$format="%YY years %MM months %DD days %HH hours %mm minutes %ss seconds"){ $date = strtotime($sdate); $date2 = strtotime($sdate2); $dif=$date2-$date; $dif=$dif<0? $dif*-1:$dif; return countTime($dif,$format); } echo datediff("2012/01/01 00:01:01","2010/05/01"); exit; result 1 years 8 months 5 days 0 hours 1 minutes 1 seconds

« previous php.notes (#194001) next »