Skip to content

Instantly share code, notes, and snippets.

@cyberdev
Last active June 21, 2021 16:35
Show Gist options
  • Save cyberdev/0e1f8270902393f1546f5aa4f7107a3d to your computer and use it in GitHub Desktop.
Save cyberdev/0e1f8270902393f1546f5aa4f7107a3d to your computer and use it in GitHub Desktop.
get time difference
<?php
function minuteDiff($str_interval, $dt_start, $dt_end, $relative = true){
if( is_string( $dt_start)) $dt_start = date_create( $dt_start);
if( is_string( $dt_end)) $dt_end = date_create( $dt_end);
$diff = date_diff( $dt_start, $dt_end, !$relative);
switch( $str_interval){
case "y":
$total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
case "m":
$total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
break;
case "d":
$total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
break;
case "h":
$total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
break;
case "i":
$total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
break;
case "s":
$total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
break;
}
if( $diff->invert)
return -1 * $total;
else
return $total;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment