1

I have this function for show friendly date timeago with unix timestamp and date format:

function friendlyDate($timestamp, $formats = null)
{

    $_DATE_FORMAT = array(
            'DAY'           => 'DAY',
            'DAY_HOUR'      => 'DAY_HOUR',
            'HOUR'          => 'HOUR',
            'HOUR_MINUTE'   => 'HOUR_MINUTE',
            'MINUTE'        => 'MINUTE',
            'MINUTE_SECOND' => 'MINUTE_SECOND',
            'SECOND'        => 'SECOND',
    );

    if ($formats == null) {
        $formats = $_DATE_FORMAT;
    }

    $seconds = time() - $timestamp;
    $minutes = floor($seconds / 60);
    $hours   = floor($minutes / 60);
    $days    = floor($hours / 24);

    if ($days > 0 && $days<=3) {
        $diffFormat = 'DAY';
    } else if($days > 3){
        return date('Y-m-d',$timestamp);
    } else {
        $diffFormat = ($hours > 0) ? 'HOUR' : 'MINUTE';
        if ($diffFormat == 'HOUR') {
            $diffFormat .= ($minutes > 0 && ($minutes - $hours * 60) > 0) ? '_MINUTE' : '';
        } else {
            $diffFormat = (($seconds - $minutes * 60) > 0 && $minutes > 0)
            ? $diffFormat.'_SECOND' : 'SECOND';
        }
    }

    $dateDiff = null;
    switch ($diffFormat) {
        case 'DAY':
            $dateDiff = sprintf($formats[$diffFormat], $days);
            break;
        case 'DAY_HOUR':
            $dateDiff = sprintf($formats[$diffFormat], $days, $hours - $days * 60);
            break;
        case 'HOUR':
            $dateDiff = sprintf($formats[$diffFormat], $hours);
            break;
        case 'HOUR_MINUTE':
            $dateDiff = sprintf($formats[$diffFormat], $hours, $minutes - $hours * 60);
            break;
        case 'MINUTE':
            $dateDiff = sprintf($formats[$diffFormat], $minutes);
            break;
        case 'MINUTE_SECOND':
            $dateDiff = sprintf($formats[$diffFormat], $minutes, $seconds - $minutes * 60);
            break;
        case 'SECOND':
            $dateDiff = sprintf($formats[$diffFormat], $seconds);
            break;
    }
    return $dateDiff;
}

I echo friendlyDate in my page like this :

echo friendlyDate(1436613754,'');

But I see this result: SECOND

What can I do to fix this problem, I would like to display date and time.

TaZz
  • 652
  • 7
  • 20
Perspolis
  • 862
  • 3
  • 11
  • 28
  • Just to make sure it's clear, can you [edit] the question to include the expected output? – IMSoP Jul 11 '15 at 11:33
  • @IMSoP: output is : `SECOND`. i need *seconds ago – Perspolis Jul 11 '15 at 12:19
  • Like I said, [edit] it into the question, and I meant the exact answer expected for the example shown. – IMSoP Jul 11 '15 at 12:34
  • Here is a functional (and upvoted) working example of what you want: http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago – JP. Aulet Mar 17 '16 at 09:17

2 Answers2

0

Here is my own timeAgo() function that I use,

function timeAgo($timestamp)
{
    //calculate here
    //@type cast
    //@current time,
    //@difference in timestamp

    $timestamp      =   (int) $timestamp;
    $current_time   =   $_SERVER['REQUEST_TIME'];
    $diff           =   $current_time - $timestamp;

    //Intervals in seconds
    $intervals  =   array (
            'year'      => 31556926,
            'month'     => 2629744,
            'week'      => 604800,
            'day'       => 86400,
            'hour'      => 3600,
            'minute'    => 60
    );

    //Now we just find the difference
    if($diff == 0)
    {
        return 'just now';
    }

    if($diff < 60)
    {
        return $diff == 1 ? $diff.' second ago' : $diff.' seconds ago';
    }

    if($diff >= 60 && $diff < $intervals['hour'])
    {
        $diff   =   floor($diff / $intervals['minute']);
        return $diff == 1 ? $diff.' minute ago' : $diff.' minutes ago';
    }

    if($diff >= $intervals['hour'] && $diff < $intervals['day'])
    {
        $diff   =   floor($diff / $intervals['hour']);
        return $diff == 1 ? $diff.' hour ago' : $diff.' hours ago';
    }

    if($diff >= $intervals['day'] && $diff < $intervals['week'])
    {
        $diff   =   floor($diff / $intervals['day']);
        return $diff == 1 ? $diff.' day ago' : $diff.' days ago';
    }

    if($diff >= $intervals['week'] && $diff < $intervals['month'])
    {
        $diff   =   floor($diff / $intervals['week']);
        return $diff == 1 ? $diff.' week ago' : $diff.' weeks ago';
    }

    if($diff >= $intervals['month'] && $diff < $intervals['year'])
    {
        $diff   =   floor($diff / $intervals['month']);
        return $diff == 1 ? $diff.' month ago' : $diff.' months ago';
    }

    if($diff >= $intervals['year'])
    {
        $diff   =   floor($diff / $intervals['year']);
        return $diff == 1 ? $diff.' year ago' : $diff.' years ago';
    }
}

echo timeAgo(1436613754);  //Outputs 8 months ago
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
0

The major problem I can see is that you don't have any formatting directives in your format strings; i.e. the values in $_DATE_FORMAT.

When the sprintf() function gets executed towards the end, the format string only contains the word "SECOND". That means the value in the $seconds variable is being ignored.

Try replacing your format array with something like this:

$_DATE_FORMAT = array(
    'DAY'           => '%d day(s)',
    'DAY_HOUR'      => '%d day(s), %d hour(s)',
    'HOUR'          => '%d hour(s)',
    'HOUR_MINUTE'   => '%d hour(s), %d minute(s)',
    'MINUTE'        => '%d minute(s)',
    'MINUTE_SECOND' => '%d minute(s), %d second(s)',
    'SECOND'        => '%d second(s)'
);

When sprintf() sees %d in the format string, it substitutes in a decimal integer value from the subsequent arguments. In this case, that's the variables such as $seconds etc. This example isn't perfect because it doesn't give the formatting a chance to pick between singular and plural, but hopefully it's a good start.

(That may not be the only problem in the function as I haven't had a chance to actually test it.)

Peter Bloomfield
  • 5,578
  • 26
  • 37