Skip to content

Instantly share code, notes, and snippets.

@ezos86
Created July 9, 2014 23:49
Show Gist options
  • Save ezos86/8ab13eff9c8a691f2c7c to your computer and use it in GitHub Desktop.
Save ezos86/8ab13eff9c8a691f2c7c to your computer and use it in GitHub Desktop.
public static function DateDiff($d1, $d2) {
if($d1 == $d2) return 0;
$d1 = (int)strtotime($d1);
$d2 = (int)strtotime($d2);
$timz = array(
"year" => (60 * 60 * 24 * 365),
"mth" => (60 * 60 * 24 * 30),
"day" => (60 * 60 * 24),
"hour" => (60 * 60),
"min" => (60)
);
$diff = abs($d1 - $d2);
$y = floor($diff / $timz["year"]);
$M = floor(($diff - ($y * $timz["year"])) / $timz["mth"]);
$d = floor((($diff - ($y * $timz["year"]) - ($M * $timz["mth"])) / $timz["day"]));
$w = ($d > 6) ? floor($d / 7) : 0;
$h = floor((($diff - ($y * $timz["year"]) - ($M * $timz["mth"]) - ($d * $timz["day"])) / $timz["hour"]));
$m = floor((($diff - ($y * $timz["year"]) - ($M * $timz["mth"]) - ($d * $timz["day"]) - ($h * $timz["hour"])) / $timz["min"]));
$s = floor($diff - (($y * $timz["year"]) + ($M * $timz["mth"]) + ($d * $timz["day"]) + ($h * $timz["hour"]) + ($m * $timz["min"])));
return array($y, $M, $w, $d, $h, $m, $s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment