There are different ways to get the time difference in minutes with PHP. We are going to discuss here two types.
1. In, First method we can Use the PHP DateTime class for calculate the difference between given two date-times. The method of DateTime class creates a Date Interval object that will calculates the difference between two date/time objects in time (total days, years, months, days, hours, minutes, seconds, etc.)
$datetime_1 = '2022-04-10 11:15:30';
$datetime_2 = '2022-04-12 13:30:45';
$start_datetime = new DateTime($datetime_1);
$diff = $start_datetime->diff(new DateTime($datetime_2));
echo $diff->days.' Days total<br>';
echo $diff->y.' Years<br>';
echo $diff->m.' Months<br>';
echo $diff->d.' Days<br>';
echo $diff->h.' Hours<br>';
echo $diff->i.' Minutes<br>';
echo $diff->s.' Seconds<br>';
Now, next we will Calculate and get the time difference in minutes:
$total_minutes = ($diff->days * 24 * 60);
$total_minutes += ($diff->h * 60);
$total_minutes += $diff->i;
echo 'Diff in Minutes: '.$total_minutes;
2. Second method we can use php strtotime() function to get the given time difference between two dates (DateTimes) in minutes using PHP.
<?php
$datetime_1 = '2022-04-10 11:15:30';
$datetime_2 = '2022-04-12 13:30:45';
$from_time = strtotime($datetime_1);
$to_time = strtotime($datetime_2);
$diff_minutes = round(abs($from_time - $to_time) / 60,2). " minutes";
0 Comments