# determining the number of months between two given dates

• Student
• Posts: 65
• Loc: kenya

3+ Months Ago

hi,
i need help with a script that can determine the number of months between any two given dates. for example 'May/2009' and 'Aug/2009'. The script should output 4.
• Lurker ಠ_ಠ
• Posts: 2733
• Loc: Nashville, TN

3+ Months Ago

Code: [ Select ]
<?php

\$start = mktime(0,0,0, 5, 1, 2009);
\$stop = mktime(0,0,0, 8, 1, 2009);

\$span = ((idate('Y', \$stop) * 12) + idate('m', \$stop)) - ((idate('Y', \$start) * 12) + idate('m', \$start)) + 1;
echo \$span;
?>
1. <?php
2.   \$start = mktime(0,0,0, 5, 1, 2009);
3.   \$stop = mktime(0,0,0, 8, 1, 2009);
4.
5.   \$span = ((idate('Y', \$stop) * 12) + idate('m', \$stop)) - ((idate('Y', \$start) * 12) + idate('m', \$start)) + 1;
6.   echo \$span;
7. ?>

• Student
• Posts: 65
• Loc: kenya

3+ Months Ago

Code: [ Select ]
\$sdate="Jan 22, 2009";
\$edate="Feb 28, 2009";

\$s=strtotime(\$sdate);
\$e=strtotime(\$edate);
echo round((\$a=((\$e-\$s)/86400)/30),0);
1. \$sdate="Jan 22, 2009";
2.    \$edate="Feb 28, 2009";
3.
4.    \$s=strtotime(\$sdate);
5.    \$e=strtotime(\$edate);
6.    echo round((\$a=((\$e-\$s)/86400)/30),0);

kindly let me know what's wrong with this. thanks anyway for the code works great.
• Born
• Posts: 1

3+ Months Ago

tiffix wrote:
Code: [ Select ]
\$sdate="Jan 22, 2009";
\$edate="Feb 28, 2009";

\$s=strtotime(\$sdate);
\$e=strtotime(\$edate);
echo round((\$a=((\$e-\$s)/86400)/30),0);
1. \$sdate="Jan 22, 2009";
2.    \$edate="Feb 28, 2009";
3.
4.    \$s=strtotime(\$sdate);
5.    \$e=strtotime(\$edate);
6.    echo round((\$a=((\$e-\$s)/86400)/30),0);

kindly let me know whats wrong with this. Thanks anyway for the code works great.

I just register, that the code served me, but to be more accurate and compare dates as

\$sdate = 2012-02-22 15: 53: 49;
\$edate = 2012-03-21 15: 53: 49;

with 1 day missing 1 month it is better to address floor as it "truncated"

which you would as a result 0 months which is correct

## Post Information

• Total Posts in this topic: 4 posts
• Users browsing this forum: No registered users and 47 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum