date_diff


php apg

RETURNS the difference between two DateTime objects.



<?php

/* - - - - - - - - - - - - - - - - - - -

    Object oriented style I
   
   - - - - - - - - - - - - - - - - - - - */

DateInterval public DateTime::diff ( DateTimeInterface $targetObject , 
                                                                             
bool $absolute = false )


where,

$targetObject = The date to compare to

$absolute 
= To control the interval

?>

<?php

/* - - - - - - - - - - - - - - - - - - -

    Object oriented style II
   
   - - - - - - - - - - - - - - - - - - - */

DateInterval DateTimeImmutable::diff ( DateTimeInterface $targetObject ,       
                                                                                    
bool $absolute = false )


where,

$targetObject = The date to compare to

$absolute 
= To control the interval

?>

<?php

/* - - - - - - - - - - - - - - - - - - - - -

    Object oriented style III
   
   - - - - - - - - - - - - - - - - - - - - - */

DateInterval public DateTimeInterface::diff ( DateTimeInterface $targetObject , 
                                                                                           
bool $absolute = false )


where,

$targetObject = The date to compare to

$absolute 
= To control the interval

?>

<?php

/* - - - - - - - - - - - - - - - - -

    Procedural style
   
   - - - - - - - - - - - - - - - - - */

DateInterval date_diff ( DateTimeInterface $baseObject , 
                                     
DateTimeInterface $targetObject , 
                                                          
bool $absolute = false )


where,

$baseObject = The date to compare to targetObject

$targetObject 
= The date to compare to $baseObject

$absolute 
= To control the interval

?>

 $baseObject 


A DateTimeInterface object to compare to the $targetObject.



 $targetObject  


A DateTimeInterface object to compare to the $baseObject.



 $absolute 


If $absolute = FALSE the DateTimeInterface interval will be forced to be NEGATIVE.

If $absolute = TRUE the DateTimeInterface interval will be forced to be POSITIVE.





The DateInterval object representing the difference between the two dates or FALSE on failure.



  1 EXERCISE   

 <?php

if(class_exists("DateTime") && 
class_exists("DateTimeZone") && 
class_exists("DateInterval"))
{
$dtm_01 = new DateTime("UTC");
$dtm_02 = new DateTime("UTC");

echo 
'<br>public DateTime add<br><br>';

$dtib = new DateInterval('P2Y0M0D');

$dtm_01_add = $dtm_01->add($dtib);
echo 
'From this moment the date will have the following value:<br>';

foreach((array)
$dtm_01->add($dtib) as $dtm01a => $dt01a)
{
    echo 
$dtm01a . ' = ' . $dt01a . '<br>';
}

echo 
'<br><br>public DateTime sub:<br><br>';

$nw_dti = new Dateinterval('P3Y2M10D');

$dtm_01_sub = $dtm_01->sub($nw_dti);
echo 
'From this moment the date will have the following value:<br>';

foreach((array)
$dtm_01->sub($nw_dti) as $dtm01b => $dt01b)
{
    echo 
$dtm01b . ' = ' . $dt01b . '<br>';
}

echo 
'<br><br>public DateInterval diff:<br>';
$dtm_01_diff = $dtm_01->diff($dtm_02, false);

$dtm_02_diff = $dtm_01->diff($dtm_02, true);

foreach((array)
$dtm_02_diff as $dtm01c => $dt01c)
{
    echo 
$dtm01c . ' = ' . $dt01c . '<br>';
}

}
else
{
echo 
"The required classes are not available in this system";
}

?> 

  2 EXERCISE   

 <?php

// full notation
$i = new DateInterval( 'P0006-01-17T06:12:30' );
// abbreviated notation
$i = new DateInterval( 'P3DT6H' );
// from a difference
$d1 = new DateTime();
$d2 = new DateTime( "2019-09-09 09:09 Asia/Tokyo" );
$i = $d1->diff( $d2 );
// displaying the difference
echo $i->format( '%a days, %h hours, %i minutes' ), "<br>";

?> 

  3 EXERCISE   

<?php
 
$dt03a 
= new DateTime('now');

$dt03b = new DateTime('2000-03-09');

$int03a = date_diff($dt03a, $dt03b, TRUE);

echo 
$int03a->format('%R%Y years or %a days');

?> 

  4 EXERCISE   

<?php

$start 
= new DateTime('1945-03-08 08:33:33');

$end   = new DateTime('now');

$int = (array)$start->diff($end);

foreach(
$int as $k => $v)
echo 
$k . ' = ' . $v . '<br>';

?>