[PHP]diff()で日数の差分を計算するサンプル

2021年4月15日

書式
public DateTime::diff ( DateTimeInterface $targetObject [, bool $absolute = FALSE ] ) : DateInterval
%R 正の値の場合(+)
%a 負の値の場合(-)

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$cftA = new DateTime('2021-03-15');
$cftB = new DateTime('2021-04-25');
$resA = $cftA->diff($cftB);
print $resA->format('%R%a');
$resB = $cftB->diff($cftA);
print $resB->format('%R%a');
?>
<?php $cftA = new DateTime('2021-03-15'); $cftB = new DateTime('2021-04-25'); $resA = $cftA->diff($cftB); print $resA->format('%R%a'); $resB = $cftB->diff($cftA); print $resB->format('%R%a'); ?>
<?php

$cftA = new DateTime('2021-03-15');
$cftB = new DateTime('2021-04-25');

$resA = $cftA->diff($cftB);
print $resA->format('%R%a'); 

$resB = $cftB->diff($cftA);
print $resB->format('%R%a');

?>

 

PHP

Posted by arkgame