Count Days Between Two Timestamps

Description: Counts the number of days between two dates that are in timestamp format (easy way is to use strtotime).
Tested Platform: PHP 5
Language: PHP
// Count the number of days between two dates
// Expects: two timestamps (strtotime)
// Returns: Number of days between them (compensates for timezone)

function countDays($time1, $time2) {
     $time1_parts = getdate($time1);
     $time2_parts = getdate($time2);
		
     $time1_new = mktime(12, 0, 0, $time1_parts['mon'], $time1_parts['mday'], $time1_parts['year']);
     $time2_new = mktime(12, 0, 0, $time2_parts['mon'], $time2_parts['mday'], $time2_parts['year']);
		
     return round(abs($time1_new - $time2_new) / 86400);	
}

Submitted: December 24, 2012

Return to the snippets listing