Help with calculating date differances

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I'm having a problem with the below code calculating the date
difference between two dates. The days should be zero, but it displays
it as 3 days. The only thing I can think that would factor in is leap
years? I just can't figure it out!

CODE:
\$DateStart = strtotime("09/11/2006");
\$DateEnd = strtotime("04/11/2009");
\$DateResult = \$DateEnd - \$DateStart;
echo "<br>y " . \$DateResult/31536000; //years
echo "<br>m " . (\$DateResult%31536000)/2592000; //months
echo "<br>w " . ((\$DateResult%31536000)%2592000)/604800; //weeks
echo "<br>d " . (((\$DateResult%31536000)%2592000)%604800)/86400; //
days
echo "<br>h " . ((((\$DateResult%31536000)%2592000)%604800)%86400)/
3600; //hours
echo "<br>m " . (((((\$DateResult
%31536000)%2592000)%604800)%86400)%3600)/60; //minutes
echo "<br>s " . ((((((\$DateResult
%31536000)%2592000)%604800)%86400)%3600)%60); //seconds

OUTPUT:
y 2.58356164384
m 7.1
w 0.428571428571
d 3
h 0
m 0
s 0

Re: Help with calculating date differances

It seems to me that you are assuming that all months have 2592000 seconds
= 30 days?

I think it would be natural to divide the months in days, but you seem to
divide the months in weeks and then look at the remaining days. The weeks
does not match up im months.

So I think that your code has two bugs, first it assumes that all months
have 30 days, then it also tries to subtract whole weeks from the
remaining days of the month. In your example the subtracted weeks is no
problem as you only subtract 0 weeks, but with other values on the dates
you would probably also suffer from that bug.

So where did your 3 days come from?

September: 30 days OK
October:   31 days, 1 day extra
November:  30 days OK
December:  31 days, 1 day extra
January:   31 days, 1 day extra
February:  28 days, -2 days extra
March:     31 days, 1 day extra

This sums to 4*1 -1 = 2 days extra, but where did your last day come from?

Your last day came from February 29 2008 which was the leap day.

regards Henrik
--
hc3(at)poolhem.se Examples of addresses which go to spammers:
root@localhost postmaster@localhost

Re: Help with calculating date differances

KoRnDragon wrote:

Why do it the hard way?  Why not use the date functions?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Help with calculating date differances

Message-ID:
KoRnDragon contained the following:

e.g.
http://www.developertutorials.com/blog/php/calculating-date-difference-more-precisely-in-php-71 /
--
Geoff Berrow  0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk

Re: Help with calculating date differances

You can not add seconds / substract seconds in order to perform date
calculations, it just does not work properly for various reasons, the leap
years etc being only one of the things.

There are many resources, see for example

http://proghowto.com/how-to-shift-a-date-object-with-1-day-in-php

Regards,
Daniel