Hi all,

I want to calculate the difference between two dates but can't achieve what I want ...

I'll explain to you what's the problem..

$date1 = "2007-02-05 20:32:15";
$date2 = date("Y-m-d H:i:s");

$diff = $date2 - $date1;

I've already done that but let's suppose if the difference is 24h then the price would be 40 Euro and every 6 hours price increases +10 Euro.

So, what I've got is when $hours variable is 24h it becomes 0 and it adds +1 to $days variable and this is the point when I'm getting confused ...

Here is the code of how I've calculated the difference

$diff = abs(strtotime($date2) - strtotime($date1)); 

		$years   = floor($diff / (365*60*60*24)); 
		$months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
		$days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
		$hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); 
		$minuts  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60); 
		$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));

Thanks in advance

Member Avatar for diafol

Does this take into account leap years and daylight saving? I got into big trouble with that before when using mutiples (365*60*60..etc)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.