4

How can I convert a timestamp difference between 2 dates

$diff = abs(strtotime($date2) - strtotime($date1));

to months and days quantity as desired ouput:

$res = 4.05 //-> 4 months and 5 days
rae1
  • 6,066
  • 4
  • 27
  • 48
Blackbeard
  • 652
  • 2
  • 8
  • 22
  • The thing is that I need to calculate how old is an ITEM based on user input of production day, month and year. The precision is months. For example, 4 years and 5 months old. The bad thing is that the server is PHP < 5.3.0 so I cannot use the DateTime diff function. – Blackbeard Feb 03 '10 at 12:05
  • You need to make PHP < 5.3 more prominent in your question, such as in the title – Myer Dec 17 '10 at 10:22

10 Answers10

8

Something like this would possibly work. My maths may be a bit off.

$diff = abs(strtotime($date2) - strtotime($date1));

define('DAY',60*60*24, true);
define('MONTH',DAY*30, true);
define('YEAR',DAY*365, true);

$years = floor($diff / (YEAR));
$months = floor(($diff - $years * YEAR) / (MONTH));
$days = floor(($diff - $years * YEAR - $months*MONTH ) / (DAY));
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • I guess there will be difference for months having 31 days. – Blackbeard Feb 03 '10 at 12:07
  • 2
    and don't ever try that for timespans containing february for which it's sometimes 1 day off, sometimes 2. Of course if the precision is not that important it would work –  Feb 03 '10 at 14:07
4

You can't approach it like that as month length varies - unless you want to make an assumption based on average month length. If you want the correct difference you need to use the actual dates then you can use php date diff - http://www.php.net/manual/en/datetime.diff.php

PhoebeB
  • 8,434
  • 8
  • 57
  • 76
3

For PHP 5.3 you might want to use
http://de.php.net/manual/en/datetime.diff.php

For lower versions you might want to use something like: (Simple solution, not fastest or best)

$days = 0;
$months = 0;
$month = date("n", $time1);
while ($time1 < $time2)
{
    ++$days;
    if (date("n", $time1) != $month)
    {
        $month = date("n", $time1);
        $days = 0;
        $months++;
    }
    $time1 = mktime(0, 0, 0, date("n", $time1), date("j", $time1) + 1, date("Y"));
}

//Disclaimer: untested

  • Should be correct now (if i didn't mix up the date() parameters again) –  Feb 03 '10 at 12:24
2

Timestamp is actually the number of seconds since Unix Epoch i.e. 1 Jan 1970. So differences in timestamps is actually seconds. So to convert seconds into days you just need to divide it by number of seconds in a day i.e. $Timestamp/(24x60x60). Same for the month $Timestamp/(24x60x60x30)

GeekTantra
  • 11,580
  • 6
  • 41
  • 55
1

The biggest problem you are going to face here is defining months. Are you going to use 4 weeks = 1 month, 30 days = 1 month, etc... I would be tempted to either leave the figure as a number of days, or at most, convert to weeks and days

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
1

I don't think this is available in php. So you will have to use integer division (or normal division) and modulo operator.

For instance (example uses hours instead of ms but it is the same trick repeated):

$totalhours = 27;
$days = round($totalhours / 24);
$hours = $totalhours % 24;

This will give $days = 1 and $hours = 3

As stated in other replies. Finding months is harder because the amount of days per month isn't fixed.

Thirler
  • 20,239
  • 14
  • 63
  • 92
  • Any chance that I can do that with a MySQL query ? – Blackbeard Feb 03 '10 at 11:51
  • Not all days have 24 hours though. – rjp Feb 03 '10 at 11:52
  • Mysql actually has several functions that can help you, this one seems to be what you are looking for (check the page for several other useful functions): http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff As for all days having 24 hours: If you ignore leap seconds, all days in UTC have 24 hours, 23 and 25 hours only happen during daylight saving time which UTC doesn't have – Thirler Feb 04 '10 at 08:28
1

You might want to take a look at DateTime.diff, as it is able to express differences you would expect from a human when comparing months (like 01.01.2010 to 01.03.2010 is a difference of (exactly) three months).

poke
  • 369,085
  • 72
  • 557
  • 602
1

You can calculate date difference in day, with a function like this

function diff_date_day($day , $month , $year , $day2 , $month2 , $year2){
  $timestamp = mktime(0, 0, 0, $month, $day, $year, 0);
  $timestamp2 = mktime(0, 0, 0, $month2, $month2, $year2);
  $diff = floor(($timestamp - $timestamp2) / (3600 * 24)); 
  return $diff; 
}

The make an assumption an average month is 30 days and calculate the number of months. It can be enough for some needs (displaying blog comment age, etc.) and totally unadapted for others.

ndequeker
  • 7,932
  • 7
  • 61
  • 93
Benoit
  • 3,569
  • 2
  • 22
  • 20
0

I know this is old now, but I came across it when looking for something to calculate the time difference in the format of "3 years & 6 months" from a given dob in timestamp format. I wrote the following which somebody might find useful.

// Determine age
$time_start = 'YOUR DOB';
$time_end = strtotime('today');
$years = 0;
$months = 0;
while ($time_start < $time_end){
    $tmp_time = strtotime('+ 1 year', $time_start);
    if ($tmp_time < $time_end){
        $years++;
        $time_start = $tmp_time;
    } else {
        $tmp_time = strtotime('+ 1 month', $time_start);
        if ($tmp_time < $time_end){
            $months++;
            $time_start = $tmp_time;
        } else {
            $time_start = $time_end;
        }
    }
}
$age = $years.' years &amp; '.$months.' months';
Luke Cousins
  • 2,068
  • 1
  • 20
  • 38
0

Found a solution using MySQL to calculate the months:

SELECT TIMESTAMPDIFF(MONTH,'{$start_time_str}','{$end_time_str}') AS m

Thanks to all involved.

Blackbeard
  • 652
  • 2
  • 8
  • 22
  • 2
    It's great that you found a solution to *your* problem, but this solution doesn't address *the problem posted in your question*, which is how to find the difference in months in PHP < 5.3. This post is still unsolved ... – Myer Dec 17 '10 at 10:27