note 111618 added to function.date-diff
| From: | billyabm at hotmail dot com | Date: | Fri, 08 Mar 2013 20:11:06 +0000 |
| Subject: | note 111618 added to function.date-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194000@lists.php.net to get a copy of this message | ||
##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
----
Server IP: 69.147.83.199
Probable Submitter: 189.154.108.214
----
Manual Page -- http://php.net/manual/en/function.date-diff.php
Edit -- https://master.php.net/note/edit/111618
Del: integrated -- https://master.php.net/note/delete/111618/integrated
Del: useless -- https://master.php.net/note/delete/111618/useless
Del: bad code -- https://master.php.net/note/delete/111618/bad+code
Del: spam -- https://master.php.net/note/delete/111618/spam
Del: non-english -- https://master.php.net/note/delete/111618/non-english
Del: in docs -- https://master.php.net/note/delete/111618/in+docs
Del: other reasons-- https://master.php.net/note/delete/111618
Reject -- https://master.php.net/note/reject/111618
Search -- https://master.php.net/manage/user-notes.php