8

Is there some function timetostr in php that will output today/tomorrow/next sunday/etc. from a given timestamp? So that timetostr(strtotime(x))=x

hakre
  • 193,403
  • 52
  • 435
  • 836
prongs
  • 9,422
  • 21
  • 67
  • 105
  • 2
    date(). See http://php.net/manual/en/function.date.php – Andrew Samuelsen Dec 25 '11 at 13:29
  • @Andypandy : I know about `date()`. I meant to ask, is there a direct function that will do reverse of `strtotime` ? – prongs Dec 25 '11 at 13:35
  • if not date, I don't know... could you provide some more context? something like this ($timestring = date('l', $timestamp) doesn't work? – Andrew Samuelsen Dec 25 '11 at 13:39
  • I wrote one. Just added a switch-case over `($timestamp-strtodate('today'))/86400`. I was just wondering whether there already was a function builtin in php. – prongs Dec 25 '11 at 13:55
  • Check this out: - http://stackoverflow.com/questions/670161/how-to-create-a-friendly-date-format-for-example-submitted-2-days-ago – Ayman Safadi Dec 25 '11 at 14:02

2 Answers2

12

This might be useful for people coming here.

/**
 * Format a timestamp to display its age (5 days ago, in 3 days, etc.).
 *
 * @param   int     $timestamp
 * @param   int     $now
 * @return  string
 */
function timetostr($timestamp, $now = null) {
    $age = ($now ?: time()) - $timestamp;
    $future = ($age < 0);
    $age = abs($age);

    $age = (int)($age / 60);        // minutes ago
    if ($age == 0) return $future ? "momentarily" : "just now";

    $scales = [
        ["minute", "minutes", 60],
        ["hour", "hours", 24],
        ["day", "days", 7],
        ["week", "weeks", 4.348214286],     // average with leap year every 4 years
        ["month", "months", 12],
        ["year", "years", 10],
        ["decade", "decades", 10],
        ["century", "centuries", 1000],
        ["millenium", "millenia", PHP_INT_MAX]
    ];

    foreach ($scales as list($singular, $plural, $factor)) {
        if ($age == 0)
            return $future
                ? "in less than 1 $singular"
                : "less than 1 $singular ago";
        if ($age == 1)
            return $future
                ? "in 1 $singular"
                : "1 $singular ago";
        if ($age < $factor)
            return $future
                ? "in $age $plural"
                : "$age $plural ago";
        $age = (int)($age / $factor);
    }
}
rich remer
  • 3,407
  • 2
  • 34
  • 47
  • I'm getting an ERROR: unexpected 'list' (T_LIST). What am I doing wrong? – hozza Sep 11 '14 at 13:54
  • Something todo with PHP versions? I got it working by declaring the list `list($singular, $plural, $factor) = $scale;` inside the foreach and replacing the list in the foreach prams with `$list` – hozza Sep 11 '14 at 14:08
  • That's correct. PHP 5.5 added "Unpacking nested arrays with list()" (http://php.net/manual/en/control-structures.foreach.php) – rich remer Sep 11 '14 at 15:27
  • Thank you! That saved me a lot of time. This should be a built in function! – SuprMan May 09 '18 at 07:44
1

There cannot be a strtotime reverse function because this is not a bijection. The source string from which you get a UNIX timestamp when you use strtotimecan be formatted in many different ways. So if you decide to reverse the function, how can you know what string format to use ? It could well be 2010-08-05 or 10 September 2000, etc. This is exactly why there is no reverse function, but as Andypandy rightly said, you have to use date()which allows you to actually define the string format you wish to end up with. I know this question is old, but I thought it deserved this answer so other users understand why there is no such function in PHP.

barakadam
  • 2,179
  • 1
  • 16
  • 15
  • 5
    whilst technically "correct" this does not answers the OP's question, rather it just furthers a negative stereotype of the all knowing programmer who knows better than the person asking the question. another approach would be to say something like this--> http://stackoverflow.com/a/3040437/830899 – unsynchronized Dec 25 '13 at 18:59
  • 12
    date("Y-m-d",time()) can do the job. – Qinjie Dec 30 '14 at 05:00