Duration between two dates and time

Hello, Today we are going to code how to get duration between two dates and time in PHP.

1. Duration between two dates.

Suppose we have two dates. Two get duration between two dates, we first need to convert date in to time stamp using function strtotime. then we will minus one date from another date.

Example :

$date1 = “01/01/2018”;

$date2 = “17/01/2018”;

$date1 = strtotime($date1);

$date2 = strtotime($date2);

$dateDiff = $date2 – $date1;

$duration = floor($dateDiff / (60 * 60 * 24));

2. Duration between two times.

Suppose we have two times. Two get duration between two times, we first need to convert time in to time stamp using function strtotime. then we will minus one time from another time.

Example :

$time1 = “13:00”;

$time2 = “17:00”;

$time1 = strtotime(date(“Y-m-d”) . ” ” . $time1);

$time2 = strtotime(date(“Y-m-d”) . ” ” . $time2);

$duration =  round(abs($time2 – $time1) / 60, 2);