开发者

PHP Convert timestamp to months and days

How 开发者_C百科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


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));


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


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


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)


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


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.


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).


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.


Found a solution using MySQL to calculate the months:

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

Thanks to all involved.


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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜