28 Mar

Find Difference Between 2 Dates in PHP

This function will find the difference between two given times. Does PHP already have a native function for that?


//The difference between 2 given time
function dateDifference(day_1,day_2) {
	diff = strtotime(day_1) - strtotime(day_2);sec   = diff % 60;diff  = intval(diff / 60);min   = diff % 60;diff  = intval(diff / 60);hours = diff % 24;days  = intval(diff / 24);
	
	return array(sec,min,hours,$days);
}

If you just want the day difference…


diff = strtotime(day_1) - strtotime(day_2) + 1; //Find the number of secondsday_difference = ceil($diff / (60*60*24)) ;  //Find how many days that is

This can be done using the DATEDIFF function in MySQL 5

[tags]php,code,date,difference,time[/tags]