編集:
class ComparableDateInterval extends DateInterval
{
/**
* Leap-year safe comparison of DateInterval objects.
*/
public function compare(DateInterval $oDateInterval)
{
$fakeStartDate1 = date_create();
$fakeStartDate2 = clone $fakeStartDate1;
$fakeEndDate1 = $fakeStartDate1->add($this);
$fakeEndDate2 = $fakeStartDate2->add($oDateInterval);
if($fakeEndDate1 < $fakeEndDate2) {
return -1;
} elseif($fakeEndDate1 == $fakeEndDate2) {
return 0;
}
return 1;
}
}
$int15 = new ComparableDateInterval('P15D');
$int20 = new ComparableDateInterval('P20D');
var_dump($int15->compare($int20) == -1); // should be true;
根拠については@fyryeの回答を参照してください(そしてそれを支持してください!)。私の最初の答えは、うるう年を安全に扱っていませんでした。
元の回答
私はこの質問に賛成票を投じましたが、受け入れられた回答には反対票を投じました。これは、PHP インストールのいずれでも機能しなかったためであり、基本的に内部で壊れた何かに依存しているためです。
代わりに、トランクに作成されなかった前述のパッチを移行しました。FWIW 最近のリリースPHP 5.6.5を確認しましたが、パッチはまだありません。コードは簡単に移植できました。唯一のことは、それがどのように比較を行うかについての警告です
$this->days が計算されている場合、それが正確であることがわかっているので、それを使用します。そうでない場合は、月と年の長さについて仮定する必要がありますが、これは必ずしも良い考えではありません。標準的な仮定があるかどうかを確認するための ISO 8601 仕様を入手できないため、月を 30 日、年を 365 日と完全に定義しましたが、そうでない場合は実際にエラーを出したい場合があります。 $this->days が利用可能ではありません。
これが例です。DateInterval
他の呼び出しから返された a を比較する必要がある場合、それを比較のソースとして使用する場合は、まずそれからcreate
aを取得する必要があることに注意してください。ComparableDateInterval
$int15 = new ComparableDateInterval('P15D');
$int20 = new ComparableDateInterval('P20D');
var_dump($int15->compare($int20) == -1); // should be true;
これがコードです
/**
* The stock DateInterval never got the patch to compare.
* Let's reimplement the patch in userspace.
* See the original patch at http://www.adamharvey.name/patches/DateInterval-comparators.patch
*/
class ComparableDateInterval extends DateInterval
{
static public function create(DateInterval $oDateInterval)
{
$oDi = new ComparableDateInterval('P1D');
$oDi->s = $oDateInterval->s;
$oDi->i = $oDateInterval->i;
$oDi->h = $oDateInterval->h;
$oDi->days = $oDateInterval->days;
$oDi->d = $oDateInterval->d;
$oDi->m = $oDateInterval->m;
$oDi->y = $oDateInterval->y;
$oDi->invert = $oDateInterval->invert;
return $oDi;
}
public function compare(DateInterval $oDateInterval)
{
$oMyTotalSeconds = $this->getTotalSeconds();
$oYourTotalSeconds = $oDateInterval->getTotalSeconds();
if($oMyTotalSeconds < $oYourTotalSeconds)
return -1;
elseif($oMyTotalSeconds == $oYourTotalSeconds)
return 0;
return 1;
}
/**
* If $this->days has been calculated, we know it's accurate, so we'll use
* that. If not, we need to make an assumption about month and year length,
* which isn't necessarily a good idea. I've defined months as 30 days and
* years as 365 days completely out of thin air, since I don't have the ISO
* 8601 spec available to check if there's a standard assumption, but we
* may in fact want to error out if we don't have $this->days available.
*/
public function getTotalSeconds()
{
$iSeconds = $this->s + ($this->i * 60) + ($this->h * 3600);
if($this->days > 0)
$iSeconds += ($this->days * 86400);
// @note Maybe you prefer to throw an Exception here per the note above
else
$iSeconds += ($this->d * 86400) + ($this->m * 2592000) + ($this->y * 31536000);
if($this->invert)
$iSeconds *= -1;
return $iSeconds;
}
}