PHP Date Difference

Finding a quick PHP function for date difference was painful.  Now that I found it, here’s the quick easy way. This site was a great help: http://www.phpf1.com/tutorial/php-date-difference.html

[code] function dateDiff($date_to_check,$unit=’d’){
$now = time();
$then = mktime(0,0,0,date(‘m’,$date_to_check),date(‘d’,$date_to_check),date(‘Y’,$date_to_check));
$diff = $now – $then;

switch($unit){
case ‘d’:
$fullDays = floor($diff/(60*60*24));
$output = $fullDays;
break;
case ‘h’:
$fullDays = floor($diff/(60*60*24));
$fullHours = floor(($diff-($fullDays*60*60*24))/(60*60));
$output = $fullHours;
break;
case ‘m’:
$fullDays = floor($diff/(60*60*24));
$fullHours = floor(($diff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($diff-($fullDays*60*60*24)-($fullHours*60*60))/60);
$output = $fullMinutes;
break;
}
return $output;
}

[/code]

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.