note 100471 deleted from datetime.diff by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 Oct 2010 14:33:00 +0000 |
| Subject: | note 100471 deleted from datetime.diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173243@lists.php.net to get a copy of this message | ||
Note Submitter: enobrev at gmail dot com
----
A modification of Dennis C's post on May 10th, 2010. This allows you to set the interval
you're looking for as well as returns a positive or negative according to whether the 2nd date
is greater than the first.
/**
* modified from http://www.php.net/manual/en/datetime.diff.php#97810
* @param DateTime $oDate1
* @param DateTime $oDate2
* @param string $sInterval (second, minute, hour, day, week, month, year)
* @return int
*/
function date_diff_interval(DateTime $oDate1, DateTime $oDate2, $sInterval) {
$iCount = 0;
switch (true) {
case $oDate1 < $oDate2:
while($oDate1 < $oDate2){
$oDate1->modify('+1 ' . $sInterval);
$iCount++;
}
break;
case $oDate1 > $oDate2:
while($oDate1 > $oDate2){
$oDate1->modify('-1 ' . $sInterval);
$iCount--;
}
break;
}
return $iCount;
}