1

I have to find total months between two dates in Unix timestamp formats. I want to create a PHP function for that. I've tried this:

get_total_month($startunixdate, $endunixdate) {
    $monthdiff = $endunixdate-$startunixdate;
    $monthdiff = $monthdiff / 60*60*24*31; 
    return $monthdiff;
}

Does this function consider leap years as well as month with 30 and 31 separately, or it will just count an approximate month?

Bojangles
  • 99,427
  • 50
  • 170
  • 208
Rahul Singh
  • 1,614
  • 6
  • 22
  • 39
  • 1
    Don't you really understand what it does? It's dividing by 60*60*24*31... – Álvaro González Jan 04 '12 at 09:07
  • 3
    It will give appr. count. For better results you can check : http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – devmach Jan 04 '12 at 09:08
  • @ÁlvaroG.Vicario : I think u should read question first. If it is not clear read it twice or thrice... – Rahul Singh Jan 04 '12 at 09:27
  • 2
    @RahulSingh - Sorry but I can't extract any other meaning: you are asking whether dividing by 31 takes into account the different month length. The answer to that is «no». If that's not your question, you should edit it and add more information. – Álvaro González Jan 04 '12 at 09:40

3 Answers3

4

Your answer is in this line;

$monthdiff = $monthdiff / 60*60*24*31

This will just count a month based on 31 days. The code divides the seconds by 60 to get the number of minutes, 60 again to get hours, 24 to get number of days, then it uses 31 as the length of a month.

This will result in an approximation of the number of months. For instance, if you take timestamps at the beginning and end of February (start of March), you will usually have a timestamp difference equivalent to 28 days (29 in a leap year). Dividing that by 31 will give you a fraction less than 1, since you are using 31 to represent a full month, when in reality a whole calendar month has passed.

Bryan
  • 2,191
  • 20
  • 28
  • That I know, Even I have wrote the above code. My question was "If it found correct amount of month or it result approx month?" – Rahul Singh Jan 04 '12 at 09:23
  • 1
    @RahulSingh It will give you an approximation only. It does not take a calendar into consideration, which is where specific rules for number of days in a month come from. I edited my answer. Hope I understood correctly. – Bryan Jan 04 '12 at 09:35
  • 1
    @RahulSingh - Isn't that what this answer tells you? Month length ranges from 28 to 31 but you always divide by 31. Conclusion: it's only an approximation. – Álvaro González Jan 04 '12 at 09:36
2

In order to do this, use the DateTime class.

function get_total_month($start, $end) {

    // Create DateTime objects
    $dt1 = new DateTime($start);
    $dt2 = new DateTime($end);

    // Get DateInterval object representing difference between the two
    $diff = $dt1->diff($dt2); // OR: $diff = date_diff($dt1, $dt2);

    // Print the "months" out
    echo $diff->format("Difference: %R%m months"); // OR access $diff->m manually
  • If I will have unix timestamp than I mean `new DateTime()` is required. I am completely dumb with classes. – Rahul Singh Jan 04 '12 at 09:25
  • I'm not sure I understand what you meant in that last comment? The function I wrote creates the DateTime objects using the $start and $end UNIX timestamps. – pbotros1234 Jan 04 '12 at 09:37
1

U can use PHP 5.3 datetime method.

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Reference: PHP DateTime

Robert Wilson
  • 659
  • 1
  • 12
  • 28