views:

945

answers:

9

How can I convert a timestamp difference between 2 dates ( $diff = abs(strtotime($date2) - strtotime($date1)); ) to months and days quantity (ouput: $res = 4.05 -> 4 months and 5 days)?

Thanks.

+1  A: 

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
+1  A: 

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
Assuming that you define 1 month as equal to 30 days.
ZombieSheep
+1  A: 

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
Any chance that I can do that with a MySQL query ?
vbklv
Not all days have 24 hours though.
rjp
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_timediffAs 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
+2  A: 

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
I guess there will be difference for months having 31 days.
vbklv
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
dbemerlin
+3  A: 

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

Unfortunately available in PHP 5 >= 5.3.0, otherwise I would of course.
vbklv
+1  A: 

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
+2  A: 

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

dbemerlin
Unfortunately returns wrong number of months. Thanks anyway.
vbklv
Should be correct now (if i didn't mix up the date() parameters again)
dbemerlin
+1  A: 

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

function diff_date_day($jour , $mois , $an , $jour2 , $mois2 , $an2){
  $timestamp = mktime(0, 0, 0, $mois, $jour, $an,0);
  $timestamp2 = mktime(0, 0, 0, $mois2, $jour2, $an2,0);
  $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.

Benoit
A: 

Found a solution using MySQL to calculate the months:

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

Thanks to all involved.

vbklv
Nice lateral thinking!