Calculate Date Difference in php

#Date Difference
$date_1 = ‘2018-02-16’;
$date_1 = ‘2018-02-18’;

$dateDifference = dateDifference($date_1, $date_2);
print_r($dateDifference);

function dateDifference($date_1 , $date_2 , $differenceFormat = ‘%a’ )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);

$interval = date_diff($datetime1, $datetime2);

return $interval->format($differenceFormat);

}

//RESULT FORMAT:
// ‘%y Year %m Month %d Day %h Hours %i Minute %s Seconds’ => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// ‘%y Year %m Month %d Day’ => 1 Year 3 Month 14 Days
// ‘%m Month %d Day’ => 3 Month 14 Day
// ‘%d Day %h Hours’ => 14 Day 11 Hours
// ‘%d Day’ => 14 Days
// ‘%h Hours %i Minute %s Seconds’ => 11 Hours 49 Minute 36 Seconds
// ‘%i Minute %s Seconds’ => 49 Minute 36 Seconds
// ‘%h Hours => 11 Hours
// ‘%a Days => 468 Days

1 thought on “Calculate Date Difference in php”

  1. Hi there, I found your website by means of Google even as searching for a
    similar topic, your site got here up, it appears good.
    I’ve bookmarked it in my google bookmarks.
    Hi there, simply changed into aware of your weblog thru Google, and located that it
    is really informative. I’m gonna be careful for brussels.
    I will appreciate in the event you continue this in future.
    Lots of people will probably be benefited from your writing.
    Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *