Search code examples
phpdatesubtraction

Php Date subtract Date and print them custom format


I have a db that I store update date as date("d-m-Y") I would like to subtrack another date from this date.

like 01-10-2015 - 04-07-2012

I would like to print result as ex 3 years 3 mounths and 3 days ago.

How can I do that?


Solution

  • Here is Your need ,

    <?php
       $date1 = new DateTime('04-07-2012'); // old date
       $date2 = new DateTime('01-10-2015'); // new date
       $interval = $date1->diff($date2); // date differ function
       echo $interval->format("%y years  %m months  %d days ago"); // formatting date 
    ?>
    

    OUTPUT: 3 years 2 months 27 days ago

    You can also refer : Date Time Difference