How to calculate minute difference between two date-times in PHP?
My solution to find the difference between two dates is here. With this function you can find differences like seconds, minutes, hours, days, years and months.
function alihan_diff_dates($date = null, $diff = "minutes") {
$start_date = new DateTime($date);
$since_start = $start_date->diff(new DateTime( date('Y-m-d H:i:s') )); // date now
print_r($since_start);
switch ($diff) {
case 'seconds':
return $since_start->s;
break;
case 'minutes':
return $since_start->i;
break;
case 'hours':
return $since_start->h;
break;
case 'days':
return $since_start->d;
break;
default:
# code...
break;
}
}
You can develop this function. I tested and works for me. DateInterval object output is here:
/*
DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 5 [s] => 13 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
*/
Function Usage:
$date = the past date, $diff = type eg: "minutes", "days", "seconds"
$diff_mins = alihan_diff_dates("2019-03-24 13:24:19", "minutes");
Good Luck.
This is how I displayed "xx times ago" in php > 5.2 .. here is more info on DateTime object
//Usage:
$pubDate = $row['rssfeed']['pubDates']; // e.g. this could be like 'Sun, 10 Nov 2013 14:26:00 GMT'
$diff = ago($pubDate); // output: 23 hrs ago
// Return the value of time different in "xx times ago" format
function ago($timestamp)
{
$today = new DateTime(date('y-m-d h:i:s')); // [2]
//$thatDay = new DateTime('Sun, 10 Nov 2013 14:26:00 GMT');
$thatDay = new DateTime($timestamp);
$dt = $today->diff($thatDay);
if ($dt->y > 0){
$number = $dt->y;
$unit = "year";
} else if ($dt->m > 0) {
$number = $dt->m;
$unit = "month";
} else if ($dt->d > 0) {
$number = $dt->d;
$unit = "day";
} else if ($dt->h > 0) {
$number = $dt->h;
$unit = "hour";
} else if ($dt->i > 0) {
$number = $dt->i;
$unit = "minute";
} else if ($dt->s > 0) {
$number = $dt->s;
$unit = "second";
}
$unit .= $number > 1 ? "s" : "";
$ret = $number." ".$unit." "."ago";
return $ret;
}
It worked on my programs, i'am using date_diff
, you can check date_diff
manual on here.
$start = date_create('2015-01-26 12:01:00');
$end = date_create('2015-01-26 13:15:00');
$diff=date_diff($end,$start);
print_r($diff);
You get results what do you want.
function date_getFullTimeDifference( $start, $end )
{
$uts['start'] = strtotime( $start );
$uts['end'] = strtotime( $end );
if( $uts['start']!==-1 && $uts['end']!==-1 )
{
if( $uts['end'] >= $uts['start'] )
{
$diff = $uts['end'] - $uts['start'];
if( $years=intval((floor($diff/31104000))) )
$diff = $diff % 31104000;
if( $months=intval((floor($diff/2592000))) )
$diff = $diff % 2592000;
if( $days=intval((floor($diff/86400))) )
$diff = $diff % 86400;
if( $hours=intval((floor($diff/3600))) )
$diff = $diff % 3600;
if( $minutes=intval((floor($diff/60))) )
$diff = $diff % 60;
$diff = intval( $diff );
return( array('years'=>$years,'months'=>$months,'days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
}
else
{
echo "Ending date/time is earlier than the start date/time";
}
}
else
{
echo "Invalid date/time data detected";
}
}
<?php
$start = strtotime('12:01:00');
$end = strtotime('13:16:00');
$mins = ($end - $start) / 60;
echo $mins;
?>
Output:
75