Thanks a million for this code i was breaking my head for the past 20 hours to get a solution but my requirement was to find the difference betweeb 2 dates in hours.I slightly modified ur end code and it satisfied my requirement
Thanks
Saravanan
Donncha Fahy wrote:
Ouch! That looks way too long winded to me 😃
This would probably be more efficient:
Before I start, these to dates are not timestamps (timestamps are the number of seconds since Jan 1970), they are simply datetime strings.
2001-09-05 14:40:55
2001-10-06 14:40:55
Using strtotime ( ) will give you a timestamp back for each datetime i.e.
$dif = strtotime ( "2001-10-06 14:40:55" ) - strtotime ( "2001-09-05 14:40:55" );
Now you'll have the difference in seconds between the 2 dates. To get the difference in days do the following:
$dif = $dif / 60 / 60 / 24;
You might get some decimal places in some cases, so it might make sense to use a rounding function i.e.
$dif = floor ( $dif / 60 / 60 / 24 );
That will give you the number of whole days, losing any extra hours on the end.