1

Possible Duplicate:
Human-readable, current time sensitive date and time formatting in PHP
Human Readable Date Using PHP

I'm looking for a way to generate human readable date references, in conversational style, such as the following:

diff( '2012-05-15', '2012-05-21' ) == "last Tuesday"
diff( '2012-05-15', '2012-05-16' ) == "yesterday"
diff( '2012-05-15', '2012-05-17' ) == "on Tuesday"
diff( '2012-04-11', '2012-05-21' ) == "on the 11th of April"

I looked into strtotime(), which seems to do the inverse of what I want. The solution doesn't need to work with future dates, just past dates. I saw another question asking the same thing for future dates in JavaScript, but it didn't really solve my problem.

Any ideas?

Community
  • 1
  • 1
Polynomial
  • 27,674
  • 12
  • 80
  • 107
  • There's nothing like that built into PHP. It shouldn't be too hard to custom code a bunch of cases though. `if (date == today - 1) "yesterday" else (date <= +1 week) "on " . weekday`... – deceze May 21 '12 at 08:34
  • @Juhana That question is basically asking for a glorified datediff. It's unrelated. – Polynomial May 21 '12 at 08:41
  • @deceze I was hoping to avoid that - there are some ugly corner cases when dealing with weeks. – Polynomial May 21 '12 at 08:41
  • @Polynomial you linked to a similiar question that does solve the described problem. What's wrong with that solution, except for you need to add 'on' word? – meze May 21 '12 at 08:44
  • @meze It's in JavaScript, not PHP, and it's also focusing on future dates, not past. It also doesn't cover the other cases I listed. – Polynomial May 21 '12 at 08:47
  • @Polynomial the accepted answer is in PHP and it does cover all cases, even future dates. (ok, maybe the last case needs an additional if). – meze May 21 '12 at 08:49
  • @meze Oh, so it does! My bad. I blame the fact that I missed my morning coffee. – Polynomial May 21 '12 at 08:50
  • @Polynomial deleted my answer. misunderstood the exact nature of your question. sorry about that :) – Darragh Enright May 21 '12 at 08:52

1 Answers1

5

I think PHP: producing relative date/time from timestamps is what you are looking for:

This is the code from the accepted answer to this question (copied it here since the original question just links to a pastebin):

function time2str($ts)
{
    if(!ctype_digit($ts))
        $ts = strtotime($ts);

    $diff = time() - $ts;
    if($diff == 0)
        return 'now';
    elseif($diff > 0)
    {
        $day_diff = floor($diff / 86400);
        if($day_diff == 0)
        {
            if($diff < 60) return 'just now';
            if($diff < 120) return '1 minute ago';
            if($diff < 3600) return floor($diff / 60) . ' minutes ago';
            if($diff < 7200) return '1 hour ago';
            if($diff < 86400) return floor($diff / 3600) . ' hours ago';
        }
        if($day_diff == 1) return 'Yesterday';
        if($day_diff < 7) return $day_diff . ' days ago';
        if($day_diff < 31) return ceil($day_diff / 7) . ' weeks ago';
        if($day_diff < 60) return 'last month';
        return date('F Y', $ts);
    }
    else
    {
        $diff = abs($diff);
        $day_diff = floor($diff / 86400);
        if($day_diff == 0)
        {
            if($diff < 120) return 'in a minute';
            if($diff < 3600) return 'in ' . floor($diff / 60) . ' minutes';
            if($diff < 7200) return 'in an hour';
            if($diff < 86400) return 'in ' . floor($diff / 3600) . ' hours';
        }
        if($day_diff == 1) return 'Tomorrow';
        if($day_diff < 4) return date('l', $ts);
        if($day_diff < 7 + (7 - date('w'))) return 'next week';
        if(ceil($day_diff / 7) < 4) return 'in ' . ceil($day_diff / 7) . ' weeks';
        if(date('n', $ts) == date('n') + 1) return 'next month';
        return date('F Y', $ts);
    }
}
Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 1
    This is a relative difference between the dates. The OP wants "human date". (i.e, "on the 11th of June", not "next month"). – meze May 21 '12 at 08:45