0

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

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by diafol
0

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)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.