2

If I have two dates - $end_date and $start_date, how can I express the difference between the two in a format such as "2 Years : 4 Months : 2 Days"?

I know that I can get the difference between the two dates like so:

$dif=strtotime($end_date)-strtotime($today);

But how can I convert that result into the human-readable format, like that shown above?

Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
Kanchana Randika
  • 550
  • 2
  • 12
  • 27
  • Can you include an example of the desired output? –  Jul 01 '11 at 02:09
  • @Tim Cooper sure, This is a simple result - 89337600 – Kanchana Randika Jul 01 '11 at 02:11
  • @Ceylo: I actually mean of the formatted timestamp. –  Jul 01 '11 at 02:12
  • @Tim Cooper sorry my mistake, 2 Years : 4 Months : 2 Days Remaining. This is what I need to output. – Kanchana Randika Jul 01 '11 at 02:15
  • possible duplicate of [PHP: Is there a function that converts a date string in a human readable format?](http://stackoverflow.com/questions/1763197/php-is-there-a-function-that-converts-a-date-string-in-a-human-readable-format) –  Jul 01 '11 at 02:24
  • @Tim Cooper no buddy that's not exactly what I wanted.I made a function by myself for that.Thanx. – Kanchana Randika Jul 01 '11 at 05:47
  • If you Google, all the answers will come to you - http://snipplr.com/view/37578/facebook-style-time-ago/ OR http://php.quicoto.com/how-to-calculate-relative-time-like-facebook/ – Luke Stevenson Jul 04 '11 at 00:23

4 Answers4

2

This is how you can format a timestamp:

echo date('d-m-Y', strtotime($end_date)) // for DD-MM-YYYY format

Are you looking to calculate the difference between 2 dates, in number days?

EDIT: code to find the difference between dates in "XXyear YYmonth ZZday". The code assumes that start and end dates are in YYYY-MM-DD format. If that's not the case for you, please either change them to YYYY-MM-DD format, OR change the arguments to mktime() accordingly.

$endDate = '2011-03-01';
$startDate = '2011-02-02';
$daysPerYear = 365;
$daysPerMonth = 30.4;
$diffDay = $diffMonth = $diffYear = 0;

$endDateTs = mktime(0, 0, 0, substr($endDate, 5, 2), substr($endDate, 8, 2), substr($endDate, 0, 4));
$startDateTs = mktime(0, 0, 0, substr($startDate, 5, 2), substr($startDate, 8, 2), substr($startDate, 0, 4));
$diffDay = ($endDateTs - $startDateTs) / 60 / 60/ 24;   // difference between 2 dates in number of days
$diffYear = floor($diffDay / $daysPerYear); // difference in years
$diffDay = $diffDay % $daysPerYear; // balance days
$diffMonth = floor($diffDay / $daysPerMonth);   // difference in months
$diffDay = ceil($diffDay % $daysPerMonth); // balance days
echo ($diffYear ? $diffYear . 'year ' : '') . ($diffMonth ? $diffMonth . 'month ' : '') . ($diffDay ? $diffDay . 'day' : '');

Note: I haven't tested the code for all possible date combinations, including leap year etc. Please feel free to tweak as needed.

Hope this helps.

Abhay
  • 6,545
  • 2
  • 22
  • 17
1

If a coarse difference is enough ("2 years ago"), then you might want to try the Date_HumanDiff PEAR package.

cweiske
  • 30,033
  • 14
  • 133
  • 194
1

The clean, DRY, professional, modern way to do this is to create two datetime objects and call diff(). The generated diff object will automatically populate year, month, day values for you.

Then you only need to iterate through a lookup array containing your desired units and implode any non-zero results into the plain English output string.

Code (Demo)

$startDate = '2001-04-20';
$endDate = '2015-11-29';
$diff = (new DateTime($startDate))->diff(new DateTime($endDate));

$lookup = [
    'y' => 'Year',
    'm' => 'Month',
    'd' => 'Day',
];

$elements = [];
foreach ($lookup as $property => $word) {
    if ($diff->$property) {
        $elements[] = "{$diff->$property} $word" . ($diff->$property !== 1 ? 's' : '');
    }
}
echo implode(' : ', $elements);
// 14 Years : 7 Months : 9 Days
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
function is_leap_year($year)
{
    if($year % 4 == 0)
    {
        if($year % 100 == 0)
        {
            if($year % 400 == 0)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    else 
    {
        return false;
    }
}

function calculate_date($now, $end){
    $years = date('Y', strtotime($end)) - date('Y', strtotime($now)) ;
    if($years < 0)
    {
        return "Error: year";
    }
    $mounths = date('m', strtotime($end)) - date('m', strtotime($now)) ;
    if($mounths < 0)
    {
        if($years < 1)
        {
            return "Error: mounth and year";
        }
        else 
        {
            $years --;
            $mounths += 12;
        }
    }
    $days = date('d', strtotime($end)) - date('d', strtotime($now)) ;
    if($days < 0){
        if($mounths < 1)
        {
            if($years < 1)
            {
                return "Error: day, mounth and year";
            }
            else 
            {
                $years --;
                $mounths += 12;
            }
        }
        else 
        {
            $mounths --;
            switch (date('m', strtotime($now)))
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    $days +=31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    $days +=30;
                    break;
                case 2:
                    if(is_leap_year(date('Y', strtotime($now))))
                    {
                        $days += 29;
                        break;
                    }
                    else
                    {
                        $days += 28;
                        break;
                    }
            }
        }
    }
    return  $years . " Years : " . $mounths . " Months : " . $days . " Days Remaining.";
}

$end_date = new DateTime('2011-08-05');
$end_date = $end_date->format('d-m-Y');
$today = date('d-m-Y');

$remaining = calculate_date($today, $end_date);

echo $remaining;

And if you want to format end_date you can use: date('d-m-Y', strtotime($end_date)) And after that you can calculate remaining time with calculate_date .