Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7177991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:49:45+00:00 2026-05-28T16:49:45+00:00

I have 2 timestamps from 2 dates: 01/2012 and 02/2013. The difference between these

  • 0

I have 2 timestamps from 2 dates: 01/2012 and 02/2013. The difference between these timestamps is 31795200. The function i’ve used is:

function unixTimeStampInMonths($timestamp){
    $elapsed_minutes = ($timestamp / 60);
    $elapsed_hours = ($elapsed_minutes / 60);
    $elapsed_days = ($elapsed_hours / 24);
    $elapsed_months = floor($elapsed_days / 30);
    return $elapsed_months;
}

But there is a problem, the months are rounded to 30 days. What’s the best way of calculating the months difference between them?

LE:

The solution suggested by a friend is:

// arguments format: 05/2010
function monthDifferenceBetween2Dates($first, $second){
    $expl1 = explode('/', $first);
    $expl2 = explode('/', $second);

    $months_in_years = ($expl2[1] - $expl1[1] - 1) * 12;
    $first_months = 12 - $expl1[0];
    $second_months = $expl2[0];

    return $months_in_years + $first_months + $second_months;
}

i’m gonna use this. thanks @nickb

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:49:46+00:00Added an answer on May 28, 2026 at 4:49 pm

    Use PHP’s DateTime class instead of UNIX timestamps:

    $start = DateTime::createFromFormat( 'm/d/Y', '01/01/2012');
    $end = DateTime::createFromFormat( 'm/d/Y', '02/01/2013');
    
    $diff = $start->diff( $end);
    var_dump( $diff);
    

    Will output:

    object(DateInterval)#3 (8) {
      ["y"]=>
      int(1)
      ["m"]=>
      int(1)
      ["d"]=>
      int(0)
      ["h"]=>
      int(0)
      ["i"]=>
      int(0)
      ["s"]=>
      int(0)
      ["invert"]=>
      int(0)
      ["days"]=>
      int(397)
    }
    

    So, to calculate total months, we can do:

    $months = $diff->y * 12 + $diff->m;
    

    Which will print 13. Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
I have 2 timestamps in JavaScript. One timestamp comes from an Ajax call to
I have an array with timestamps. If this timestamps are between two given dates
I want to copy files that have timestamps from the time the script begins
I have timestamp in milliseconds from 1970. I would like to convert it to
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
I have a Timestamp value that comes from my application. The user can be
I have an application that currently stores timestamps in MySQL DATETIME and TIMESTAMP values.
I have a requirement to store dates and durations arising from multiple different calendars.
What is the simplest way to compare two dates neglecting the timestamps. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.